简体   繁体   English

如何将数据从列表视图从一个片段发送到另一个

[英]How to send data from listview from one fragment to another

I have a couple listview items and I need when I click on particular listview to go to Fragment_2 with a value of listview that is clicked. 我有几个listview项,当我单击特定的listview时,需要使用被单击的listview值转到Fragment_2。 I try code below on Fragment_1 I get the proper value an it show in Tosat notification, but in Fragment_2 bundle is always null. 我在Fragment_1上尝试以下代码,我得到了正确的值,它在Tosat通知中显示,但在Fragment_2包中始终为null。

In onCreate method in Fragment_1 在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);

In onCreate method in Fragment_2 在Fragment_2中的onCreate方法中

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

UDPATE : 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 .......
            }
        });

Just replace below code in onItemClick method. 只需替换下面的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);

On MainActivity replace your MainFragment like this, 在MainActivity上,像这样替换MainFragment,

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

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

you are not committing fragment 你没有提交片段

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

You are starting the Activity "MainActivity.class" and passing your bundle to a Fragment class "MainFragment" but never actually starting the MainFragment. 您正在启动活动“ MainActivity.class”,并将捆绑软件传递给Fragment类“ MainFragment”,但实际上从未启动过MainFragment。 So, it is obvious that your code won't work 因此,很明显,您的代码将无法正常工作

You are calling activity so how can you get the data in the fragment and if you are doing like this : 您正在调用活动,以便如何获取片段中的数据以及如果您这样做的话:

First you are launching the activity by this 首先,您要以此启动活动

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

Then in this activity you are launching the fragment 然后在此活动中,您将启动片段

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

Than in this case pass the data to the intent first like this 比在这种情况下首先将数据传递给意图

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

and pass this data to the fragment by using bundles Hope it will work for you . 并使用捆绑包将该数据传递到片段希望它对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM