我在一个片段中有一个Bundle,它将一个字符串传递给另一个片段。此字符串需要在文本视图中设置文本,我的方法不起作用。我不确定为什么,但我的所有其他字符串都通过了。
请查看我的代码,让我知道我的错误 - 我不明白......
自:
public void onClick(View v) {
Bundle args = new Bundle();
FragmentManager fm = getFragmentManager();
final FragmentTransaction vcFT = fm.beginTransaction();
vcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);
switch (v.getId()) {
case R.id.regulatoryBtn :
String keyDiscriptionTitle = "Regulatory Guidance Library (RGL)";
args.putString("KEY_DISCRIPTION_TITLE", keyDiscriptionTitle);
RegulatoryDiscription rd = new RegulatoryDiscription();
vcFT.replace(R.id.viewContainer, rd).addToBackStack(null).commit();
rd.setArguments(args);
break;
. . .
}
要:
public class RegulatoryDiscription extends Fragment {
Bundle args = new Bundle();
String DNS = "http://192.168.1.17/";
String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.discription_view, container, false);
TextView title = (TextView) view.findViewById(R.id.discriptionTitle);
String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE);
title.setText(keyDiscriptionTitle);
return view;
}
. . .
}
答案 0 :(得分:4)
您在RegulatoryDescription片段中将args声明为新的Bundle。这将初始化一个完全为空的新Bundle对象
您需要检索传入的现有参数。
离。
public class RegulatoryDiscription extends Fragment {
Bundle args;
String DNS = "http://192.168.1.17/";
String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.discription_view, container, false);
args = getArguments(); //gets the args from the call to rd.setArguments(args); in your other activity
TextView title = (TextView) view.findViewById(R.id.discriptionTitle);
String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE);
title.setText(keyDiscriptionTitle);
return view;
}
}