【问题标题】:How to send data from listview from one fragment to another如何将列表视图中的数据从一个片段发送到另一个片段
【发布时间】:2019-10-29 15:53:53
【问题描述】:

我有几个列表视图项目,当我单击特定列表视图以转到具有被单击列表视图值的 Fragment_2 时,我需要。我在 Fragment_1 上尝试下面的代码,我得到了正确的值,它显示在 Tosat 通知中,但在 Fragment_2 中,捆绑始终为空。

在 Fragment_1 的 onCreate 方法中

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
                MainFragment fragment = new MainFragment ();
                Bundle bundle = new Bundle();
                bundle.putString("view", value);
                Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
                fragment.setArguments(bundle);

在 Fragment_2 的 onCreate 方法中

 Bundle bundle = this.getArguments();
        if (bundle != null) {
            String myString = bundle.getString("view");
            Toast.makeText(getContext(), myString, Toast.LENGTH_SHORT).show();

UDPATE

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
                MainFragment fragment = new MainFragment ();
                Bundle bundle = new Bundle();
                bundle.putString("view", value);
                Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
                fragment.setArguments(bundle);

                if (position == 0) {
                    Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                    startActivityForResult(myIntent, 0);

                }

                if (position == 1) {
                    Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 2) {
                    Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 3) {
                    Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                    startActivityForResult(myIntent, 0);
                }

                            // ETC .......
            }
        });

【问题讨论】:

  • 添加setOnItemClickListener的完整代码。这样我就可以检查您是否使用正确的方法来替换片段。
  • 我更新了代码
  • 我看过你的代码。你没有使用片段。您在点击时打开新活动。所以你不会在其他活动中获得该片段的价值。
  • 我打开该活动的另一个片段
  • 然后你必须在新活动中传递数据,然后将其发送到目标片段。

标签: java android listview android-fragments bundle


【解决方案1】:

只需替换 onItemClick 方法中的以下代码即可。

            String value = ((TextView) 
            view.findViewById(R.id.username)).getText().toString();
            Bundle bundle = new Bundle();
            bundle.putString("view", value);
            Intent myIntent = new Intent(view.getContext(), MainActivity.class);
            myIntent.putExtra(bundle);
            startActivityForResult(myIntent, 0);

在 MainActivity 上像这样替换你的 MainFragment,

            MainFragment fragment = new MainFragment ();
            fragment.setArguments(getIntent().getExtras());

            getSupportFragmentManager()
              .beginTransaction()
              .replace("your fragment container id here", fragment)
              .commit();

【讨论】:

  • 我收到.replace(R.id.container, fragment) 不兼容类型的错误:MainFragment 无法转换为 Fragment
  • @ValentinGjorgoski 请参考链接,你会得到你的解决方案 [stackoverflow.com/questions/27037662/…
  • 答案已更新以支持带有片段的活动导航。请使用该代码。
  • 在 onItemClick 方法中。我替换了代码,但意图有错误intent.putExtra(bundle);error: cannot find symbol variable intent
  • 我解决了。我将 intent.putExtra(bundle); 更改为 myIntent.putExtra("view", value); 谢谢它现在可以工作了!
【解决方案2】:

你没有提交片段

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
                MainFragment fragment = new MainFragment ();
                Bundle bundle = new Bundle();
                bundle.putString("view", value);
                Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
                fragment.setArguments(bundle);

// This line is important
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();

【讨论】:

    【解决方案3】:

    您正在启动活动“MainActivity.class”并将您的包传递给片段类“MainFragment”,但从未真正启动 MainFragment。 所以,很明显你的代码是行不通的

    【讨论】:

      【解决方案4】:

      您正在调用活动,那么如何获取片段中的数据,如果您这样做:

      首先你通过这个来启动活动

        Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                      startActivityForResult(myIntent, 0);
      

      然后在这个活动中你将启动片段

      getFragmentManager().beginTransaction().replace("your fragment container id here", 
      fragment).commit();
      

      在这种情况下,首先像这样将数据传递给意图

        Intent myIntent = new Intent(view.getContext(), MainActivity.class);
           myIntent.putString("data","Put the data which you want to pass");
                      startActivityForResult(myIntent, 0);
      

      并通过使用捆绑将此数据传递给片段 希望它对你有用。

      【讨论】:

      • 你能解释得更具体一点吗?我不明白要改变什么以及在哪里改变
      • 你能告诉你从哪里启动你的片段'
      • 在 MainActivity 我有 setContentView(R.layout.activity_main);我用 和 MainFragment public View onCreateView( LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false);我不知道这是正确的方式,我对 android 了解一点
      • 先替换你的intent Intent myIntent = new Intent(view.getContext(), MainActivity.class); myIntent.putString("数据",值); startActivityForResult(myIntent, 0);
      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多