[問題] fragment要怎麼傳值給fragment

看板AndroidDev作者 (叛帝)時間11年前 (2012/11/30 07:01), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
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); ///////////////////Error SAS cannot be resolved to a variable ///////////////// return view; } 有沒有版友能給些建議呢? 完全不知道該怎麼做XD -- ══ ◣◢◣◢Vocaloid╮ ◤◥ ◢◢ quetzal◥◥ ◥◤ ◣◢ ║ ║ ● ●▼ ● ●◥ ● ● ; ︽﹨ ◥▼ ▃▃ ║ ● ● ● ● ド! ███▆≡ ▅▅ ══◣︶●══ ◥◣ / / / ══ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.34.127.207

11/30 07:39, , 1F
下面的SAS有在接值的fragment裡被定義嗎?
11/30 07:39, 1F

11/30 08:37, , 2F
好像沒有orz定義式該怎麼寫呢?
11/30 08:37, 2F

11/30 08:59, , 3F
String SAS="50";
11/30 08:59, 3F

11/30 08:59, , 4F
你傳值的fragment有寫
11/30 08:59, 4F

11/30 09:20, , 5F
所以我在接值的地方補一條 String SAS = null; ?
11/30 09:20, 5F

11/30 09:49, , 6F
看你要給他什麼預設值 在bundle取值失敗的時候
11/30 09:49, 6F

11/30 19:10, , 7F
http://developer.android.com 上就有寫作法了阿
11/30 19:10, 7F
文章代碼(AID): #1Gj-bXMX (AndroidDev)