2017-08-25 94 views
0

我有捆绑对象的问题。当我通过数据(onCLick按钮)从活动到片段我得到了捆绑对象的值,并在我将值传递给适配器后...我有问题:捆绑对象再次从活动中获取值(但现在值为空),并通过空值到适配器...我不知道为什么第一次束之后,从活动得到再次值...我希望你能帮助我捆绑对象问题

我告诉我的玩具代码

活动:

 Bundle bundle = new Bundle(); 
      bundle.putIntegerArrayList("oki", hm); 
      bundle.putIntegerArrayList("okiquantitapizze", hm_quantitàpizze); 

      System.out.println("PERO:" + bundle); 
/* 
      MyListFragment2 myFragment = new MyListFragment2(); 
      myFragment.setArguments(bundle); 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.pero, myFragment); 
      transaction.commit();*/ 

      MyListFragment myFragment = new MyListFragment(); 
      myFragment.setArguments(bundle); 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.a, myFragment); 
      transaction.commit(); 

片断:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     System.out.println("BUNDLES1 prima:" + bundle); 


     bundle = getArguments(); 

     System.out.println("BUNDLES1 dopo:" + bundle); 
     if (bundle != null) { 


      strtext2 = bundle.getIntegerArrayList("oki"); 
      quantitàpizze2=bundle.getIntegerArrayList("okiquantitapizze"); 
      System.out.println("CAZZ2:" + strtext2); 

      System.out.println("PRESO2:" + quantitàpizze2); 




     } 



} 
+0

你可以粘贴你的整个活动?还有,当包再次通过时,你正在做什么?你是否正在旋转你的屏幕,来自另一个应用程序等? –

+0

不,当我点击按钮之前传递数据正确的方式,并在重新计算数据.....所以片段创建两次.... – androidiano

+0

任何消息呢? –

回答

0

试试这个:
给你的片段添加一个静态的newInstance()方法。

public static MyListFragment newInstance(ArrayList<Integer> list1,ArrayList<Integer> list2) { 
MyListFragment myListFragment = new MyListFragment(); 

Bundle bundle = new Bundle(); 
bundle.putIntegerArrayList("oki", list1); 
bundle.putIntegerArrayList("okiquantitapizze", list2); 
myListFragment.setArguments(bundle); 

return myListFragment; 
} 

和实例片段是这样的:

 MyListFragment myFragment = MyListFragment.newInstance(list1,list2); 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.a, myFragment); 
     transaction.commit(); 
+0

我试过但我有同样的问题..我不知道为什么 – androidiano

+0

我把片段放入包含片段的活动布局 – androidiano

0

我的建议是不要重建片段,有没有需要。添加标签时添加标签。

transaction.replace(R.id.a, myFragment, "MyListFragment"); 

在您创建分段这样的方法:

public void updateValues(ArrayList<Integer> list1, ArrayList<Integer> list2){ 
    //do your stuff and update the UI 
} 

然后在的onClick()只要找到片段并调用该方法。

public void onClick(View view) { 
     MyListFragment fragment = (MyListFragment) manager.findFragmentByTag("MyListFragment"); 
     if (fragment != null && fragment.isVisible()) { 
      fragment.updateValues(list1, list2); 
     } else { 
      Bundle bundle = new Bundle(); 
      bundle.putIntegerArrayList("oki", list1); 
      bundle.putIntegerArrayList("okiquantitapizze", list2); 
      fragment.setArguments(bundle); 
      manager.beginTransaction() 
        .replace(R.id.a, fragment, "MyListFragment") 
        .commit(); 
     } 
    }