如何从一个片段数据发送到另一个片段? [英] How to send data from one Fragment to another Fragment?
问题描述
您好我知道有这个问题的答案。我已经尝试了所有的人,但它不工作在我的应用程序。 我开发一个应用程序,有3个片段的活性。首先片段展示了一个网站,第二个片段有ListView和第三个片段还有一个列表视图。现在,我想从第三个片段首先片段发送URL,当用户点击listitem..This是我做了什么。
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);}
});
这是第一个片段,我需要的网址,并希望在web视图中显示。
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);
当我点击列表项我什么也没看到。它不重定向我的第一个fragment.Please帮助我..
When I click on list item I don't see anything . It does not redirect me to the first fragment.Please help me..
推荐答案
使用捆绑发送字符串:
//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");
这篇关于如何从一个片段数据发送到另一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!