如何将数据从一个 Fragment 发送到另一个 Fragment? [英] How to send data from one Fragment to another Fragment?
问题描述
我知道这个问题有答案.我已经尝试了所有这些,但它在我的应用程序中不起作用.我正在开发一个具有 3 个 Fragment 活动的应用程序.第一个片段显示一个网站,第二个片段有列表视图,第三个片段有另一个列表视图.现在我想在用户点击列表项时将 URL 从第三个片段发送到第一个片段..这就是我所做的.
Hi I know there are answers of this question. I have tried all of them but it is not working in my app. I am developing an app that has 3 Fragment activity. First fragment shows a website, second fragment has listview and third Fragment has another listview. Now I want to send URL from third fragment to first fragment when user clicks on listitem..This is what I have done.
我正在将字符串 url 从此片段发送到第一个片段.
I am sending string url from this Fragment to first fragment.
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
这是我需要网址并希望在 webview 中显示的第一个片段.
This is first fragment where I need the url and Want to show in the webview.
String url="http://www.hotelsearcher.net/";
Bundle args = getArguments();
if (args != null){
url = args.getString("position");
}
WebView webView= (WebView) V.findViewById(R.id.webView1);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginState(PluginState.ON);
webView.loadUrl(url);
当我点击列表项时,我什么也没看到.它不会将我重定向到第一个片段.请帮助我..
When I click on list item I don't see anything . It does not redirect me to the first fragment.Please help me..
推荐答案
使用Bundle发送String
:
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
在新片段的onCreateView
中:
In onCreateView
of the new Fragment:
//Retrieve the value
String value = getArguments().getString("YourKey");
这篇关于如何将数据从一个 Fragment 发送到另一个 Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!