作者tga123 (叛帝)
看板AndroidDev
标题[问题] fragment要怎麽传值给fragment
时间Fri Nov 30 07:01:48 2012
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
--
ボ ◢ ◥══
◢ ◥═══
◢ ◥◣ ══
◥◣◢◤◣
═ ◣◢═Vocaloi
d╮
║
◣ ◤◥ ◢◢◢▼ quetzal◤◥◥﹨◣ ◢◥◤ ◥ ▼◥ ◣◢ ║
║
● ●▼ ● ●◥ ロ ● ● ;│ ▼◥︽﹨ ◆▼◥▼﹨ ▃▃
║
◣▄ ◢◤ ◤◣▆ ◢◥ ◥
◣▃ ▉
◤ ═● ● ▌ ◤● ● ▌ ◢
ド!
║
◤◤◢ ◣
カ ████▆≡ ◤▆ ◥ ◢
◣▁ ▉
◥ ◣▄ ▉
◤ ▅▅
╰══
◣︶●═══
▊█▎◥◣══
▼ ◢/△ ◤═イ═△▅/◥══
△▅/◣ ═══
╯
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.34.127.207
1F:→ sunghau:下面的SAS有在接值的fragment里被定义吗? 11/30 07:39
2F:→ tga123:好像没有orz定义式该怎麽写呢? 11/30 08:37
3F:→ sunghau:String SAS="50"; 11/30 08:59
4F:→ sunghau:你传值的fragment有写 11/30 08:59
5F:→ tga123:所以我在接值的地方补一条 String SAS = null; ? 11/30 09:20
6F:→ sunghau:看你要给他什麽预设值 在bundle取值失败的时候 11/30 09:49