如何将字符串数据从活动传递到片段? [英] how to pass string data from activity to fragment?
本文介绍了如何将字符串数据从活动传递到片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将字符串值从活动传递到片段.为此,我使用捆绑软件来传输字符串值.
I want to pass string value from activity to fragment. For that I'm using bundle for transferring string value.
输入STRING活动 传递字符串值:-
Bundle bundle = new Bundle();
bundle.putString("Value", resultp.get(CurrentProjectActivity.VALUE));
Log.d(TAG, "Value ::: " + resultp.get(CurrentProjectActivity.VALUE));
// set Fragmentclass Arguments
AmenetiesFragment fragobj = new AmenetiesFragment();
fragobj.setArguments(bundle);
在日志中,我也获得了值"值.
In log I got "Value" value as well.
获取片段中的字符串值(不起作用).
GET STRING VALUE IN FRAGMENT (IT IS NOT WORKING).
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_listview, container, false);
Bundle bundle = this.getArguments();
Log.d(TAG, "Value's value:) ::: " + bundle);
String strtext = bundle.getString("Value");
return rootView;
}
在日志中,我得到BUNDLE的NULL值.请帮助我解决此问题.预先感谢.
In log I'm getting NULL value for BUNDLE. Please help me to resolve this Issue. Thanks in advance.
推荐答案
I advice you to follow this link1 and this . You should use interface. Check here
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
以及Frament任务
and in the Frament task
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
这篇关于如何将字符串数据从活动传递到片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文