【问题标题】:How to send data from one Fragment to another Fragment?如何将数据从一个 Fragment 发送到另一个 Fragment?
【发布时间】:2014-08-24 16:17:15
【问题描述】:

您好,我知道这个问题有答案。我已经尝试了所有这些,但它在我的应用程序中不起作用。 我正在开发一个具有 3 个片段活动的应用程序。第一个片段显示一个网站,第二个片段有列表视图,第三个片段有另一个列表视图。现在我想在用户点击列表项时将 URL 从第三个片段发送到第一个片段。这就是我所做的。

我正在从这个 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 中显示的第一个片段。

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);

当我单击列表项时,我什么也看不到。它不会将我重定向到第一个片段。请帮助我..

【问题讨论】:

    标签: android android-fragments android-listview android-webview


    【解决方案1】:

    使用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 的新片段中:

    //Retrieve the value
    String value = getArguments().getString("YourKey");
    

    【讨论】:

    • YourNewFragment 是什么意思?
    • 是你的“另一个片段”。是你想要接收String值的fragment。
    • 容器指的是什么?
    • 是你的活动xml文件中声明的id。 android:id="@+id/container"
    • 好的..它有效..它重定向到第一个片段。但它显示对话框选择浏览器来查看网站,而不是在我的 webview 上显示网站..你能帮我吗这个。我会接受答案。:)
    【解决方案2】:

    1.如果片段由相同的活动托管 - 您不能将意图投射到片段。 Fragment 是 Activity 的一部分,它本身并不是一个 Activity。因此,要在片段之间共享字符串,您可以在 Activity 中声明一个静态字符串。从片段 A 访问该字符串以设置值并获取片段 B 中的字符串值。

    2.两个片段都由不同的活动托管-然后您可以使用 putExtra 将字符串从 Activity A 的片段 A 传递给 Activity B。将该字符串存储在 Activity B 中并在片段 B 中使用。

    【讨论】:

    • 我有不同的片段,我想将字符串从片段发送到另一个片段。
    • 是的,理解,但是这两个片段都由同一活动托管??
    【解决方案3】:

    您必须将您的捆绑包附加到您的片段。

    fragment.setArguments(bundle);
    

    然后更改或替换新片段。

    您必须确保字符串在新片段中。调试中!!

    【讨论】:

      【解决方案4】:

      您可以通过两种方式发送数据 首先,当您想在发送数据时启动该片段时

      SecondFragment ldf = new SecondFragment ();
      Bundle args = new Bundle();
      args.putString("YourKey", "YourValue");
      ldf.setArguments(args);
      
      //Inflate the fragment
      getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
      

      其次,当你想在不打开片段的情况下发送数据时

      FragmentTransaction ft = mFragmentManager.beginTransaction();
      ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST);
      ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST);
      ft.commit();
      
      Fragment fragment = mFragmentManager.findFragmentByTag(MainActivity.FRAG_MODEL_LIST);
      Log.d("MY", "found fragment: " + (fragment != null));
      

      【讨论】:

      • 如何接收第二个片段中的数据?
      猜你喜欢
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2011-11-01
      相关资源
      最近更新 更多