Android 台灣中文網

標題: fragment要怎麼傳值給fragment? [打印本頁]

作者: hl2dm    時間: 2012-11-30 09:34
標題: fragment要怎麼傳值給fragment?
Activity只要用intent結合bundle

就能成功達到傳值的要求

但是換做是fragment好像就不適這麼容易...

我有嘗試照著網上範例去做
但好像不太行

傳值程式碼
  1. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  2.             Bundle savedInstanceState) {
  3.         super.onCreateView(inflater, container, savedInstanceState);
  4.         View view = inflater.inflate(R.layout.templistmune, container, false);
  5. FragmentManager DS2 = getFragmentManager();
  6.     FragmentTransaction DSE2 = DS2.beginTransaction();
  7.     Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
  8.     if (DF2 == null) {
  9.         String title = "Fragment A";
  10.         templistview2 usermune = new templistview2(title);
  11.      DSE2.add(R.id.frameLayout4, usermune);
  12.         DSE2.addToBackStack(null);
  13.         DSE2.commit();
  14. /////////////////////////////////////////////////////////
  15.         Bundle bundle = new Bundle();
  16.         String SAS="50";
  17.         bundle.putString("ST", SAS);
  18.         DF2.setArguments(bundle);
  19. ////////////////////////////////////////////////
  20.         }
複製代碼
接收值的程式碼
  1. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  2.             Bundle savedInstanceState) {
  3.         super.onCreateView(inflater, container, savedInstanceState);
  4.         View view = inflater.inflate(R.layout.templistmune2, container,
  5. false);

  6. //////////////////////////////////////////////////////////////////////////////\
  7. //////
  8.      Bundle bundle = this.getArguments();
  9.     String myST = bundle.getString("ST", SAS);
  10. ////////////////////如果定義String SAS=null;就會報錯///////////////////////////////////////
  11. ///////////////////Error SAS cannot be resolved to a variable
  12. /////////////////



  13.         return view;

  14.     }
複製代碼
有沒有版友能給些建議呢?
完全不知道該怎麼做XD
作者: ploglin    時間: 2012-11-30 10:55
還沒試過 fragment

下面的 document 不知道對你有沒有幫助
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1