How to send data from listview from one fragment to another

Valentin Gjorgoski :

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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130583&siteId=1