2017-08-25 108 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(); 
     } 
    }