Android 台灣中文網
標題:
fragment要怎麼傳值給fragment?
[打印本頁]
作者:
hl2dm
時間:
2012-11-30 09:34
標題:
fragment要怎麼傳值給fragment?
Activity只要用intent結合bundle
就能成功達到傳值的要求
但是換做是fragment好像就不適這麼容易...
我有嘗試照著網上範例去做
但好像不太行
傳值程式碼
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune, container, false);
FragmentManager DS2 = getFragmentManager();
FragmentTransaction DSE2 = DS2.beginTransaction();
Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
if (DF2 == null) {
String title = "Fragment A";
templistview2 usermune = new templistview2(title);
DSE2.add(R.id.frameLayout4, usermune);
DSE2.addToBackStack(null);
DSE2.commit();
/////////////////////////////////////////////////////////
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
////////////////////////////////////////////////
}
複製代碼
接收值的程式碼
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune2, container,
false);
//////////////////////////////////////////////////////////////////////////////\
//////
Bundle bundle = this.getArguments();
String myST = bundle.getString("ST", SAS);
////////////////////如果定義String SAS=null;就會報錯///////////////////////////////////////
///////////////////Error SAS cannot be resolved to a variable
/////////////////
return view;
}
複製代碼
有沒有版友能給些建議呢?
完全不知道該怎麼做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