您好,我知道这个问题有答案。我已经尝试了所有这些,但它在我的应用程序中不起作用。 我正在开发一个具有 3 个 fragment Activity 的应用程序。第一个 fragment 显示一个网站,第二个 fragment 有 ListView ,第三个 fragment 有另一个 ListView 。现在我想在用户点击列表项时将 URL 从第三个 fragment 发送到第一个 fragment 。这就是我所做的。
我正在从这个 Fragment 向第一个 Fragment 发送字符串 url。
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);}
});
这是我需要 url 并希望在 webview 中显示的第一个 fragment 。
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 。请帮助我..
最佳答案
使用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();
在新Fragment的onCreateView
中:
//Retrieve the value
String value = getArguments().getString("YourKey");
关于android - 如何将数据从一个 Fragment 发送到另一个 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24555417/