简体   繁体   中英

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. 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.

In onCreate method in Fragment_1

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

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

Just replace below code in onItemClick method.

            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,

            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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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