2015-10-03 186 views
0

我跟着谷歌教程,並沒有得到我的代碼中的任何錯誤,但由於某種原因,我的包中的數據似乎並沒有從我的主要活動發送到我的片段(我的應用程序只是不斷返回「0.00」)。請幫忙。從主活動發送包到片段

主要活動:

@Override 
public void assignButtonBWHW1Clicked(String assignButtonBWHWClicked) { 

    if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) { 

     assignFragment1 = new assignFragment1(); 
     buttonsGeneric = new genericBackNextFragment(); 

     splitItFragmentManager3 = getFragmentManager(); 
     splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction(); 
     splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1); 

     splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric); 
     splitItFragmentTransaction3.addToBackStack(null); 

     splitItFragmentTransaction3.commit(); 

     Bundle bundleAssign = new Bundle(); 
     bundleAssign.putDouble("price1", 22); 
     assignFragment1.setArguments(bundleAssign); 

    } 
} 

片斷:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    assignFragment1View = inflater.inflate(R.layout.assign_fragment_1, container, false); 

    item1 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_1); 
    item2 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_2); 
    priceItem1 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_1); 
    priceItem2 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_2); 

    Bundle bundleAssign = this.getArguments(); 
    Double price1Double = bundleAssign.getDouble("priceItem1", 0.00); 
    priceItem1.setText(String.valueOf(price1Double)); 

    return assignFragment1View; 
} 

回答

1

剛剛更新的fragmentkeypriceItem1price1

Double price1Double = bundleAssign.getDouble("priceItem1", 0.00); 

Double price1Double = bundleAssign.getDouble("price1", 0.00); 

爲了讓事情發生clear.this因爲您正在使用單獨的按鍵,同時增加數據包中,使用不同的關鍵字進行檢索。所以它正在返回default value,即0.00priceItem1密鑰

+0

謝謝非常!這解決了它。如何讓你可以百遍查看自己的代碼,而不是發現如此明顯的內容,這真是太神奇了。 –

+0

修好了,接受它,如果它幫助:)所以它從未回答的問題隊列中清除:) –

0

嘗試

Bundle bundleAssign = getArguments(); 

或者,
Bundle bundleAssign = getActivity().getArguments()

Bundle bundleAssign = this.getArguments(); 

獲得價值代替像

double result = bundleAssign.getDouble("price1"); 
0

嘗試把包初始化到片段你片段nmanager之前。

if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) { 

    assignFragment1 = new assignFragment1(); 
    buttonsGeneric = new genericBackNextFragment(); 

    Bundle bundleAssign = new Bundle(); 
    bundleAssign.putDouble("price1", 22); 
    assignFragment1.setArguments(bundleAssign); 

    splitItFragmentManager3 = getFragmentManager(); 
    splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction(); 
    splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1); 

    splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric); 
    splitItFragmentTransaction3.addToBackStack(null); 

    splitItFragmentTransaction3.commit(); 

} 
1

兩次更改你將不得不在你的代碼中執行它。
第一個是前交易

if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) { 

     assignFragment1 = new assignFragment1(); 
     Bundle bundleAssign = new Bundle(); 
     bundleAssign.putDouble("price1", 22); 
     assignFragment1.setArguments(bundleAssign); 
     buttonsGeneric = new genericBackNextFragment(); 

     splitItFragmentManager3 = getFragmentManager(); 
     splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction(); 
     splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1); 

     splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric); 
     splitItFragmentTransaction3.addToBackStack(null); 

     splitItFragmentTransaction3.commit(); 



    } 

設定的參數,第二個總是使用相同的密鑰對您的包​​內設置和檢索數據的片段,從而改變

Double price1Double = bundleAssign.getDouble("priceItem1", 0.00); 

Double price1Double = bundleAssign.getDouble("price1", 0.00); 
+0

非常感謝你非常非常。在我的情況下,我沒有調用'assignFragment1.setArguments(bundleAssign);'(只是一個註釋:在你的答案中使這個粗體,因爲這是你的答案和網上任何其他nas的主要區別)。再次感謝 –