1

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 .......
            }
        });
RKRK
  • 1,284
  • 5
  • 14
  • 18

4 Answers4

1

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();
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
  • I get that error for `.replace(R.id.container, fragment)` incompatible types: MainFragment cannot be converted to Fragment – Valentin Gjorgoski Jun 15 '19 at 06:51
  • 1
    @ValentinGjorgoski Please refer to the link, you will get your solution [https://stackoverflow.com/questions/27037662/incompatible-types-homefragment-cannot-be-converted-to-fragment-in-android] – Nasreen Ustad Jun 15 '19 at 07:01
  • 1
    Answer updated to support activity navigation with fragments. Please use that code. – DHAVAL A. Jun 15 '19 at 07:07
  • in onItemClick method. I replace the code and I have error with intent `intent.putExtra(bundle);` `error: cannot find symbol variable intent` – Valentin Gjorgoski Jun 15 '19 at 08:23
  • I solve it. I change `intent.putExtra(bundle);` to `myIntent.putExtra("view", value);` Thanks it's work now! – Valentin Gjorgoski Jun 15 '19 at 10:27
1

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

Nasreen Ustad
  • 1,564
  • 1
  • 19
  • 24
0

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

Ashutosh Patoa
  • 294
  • 4
  • 20
0

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 .

  • Can you explain a little more specific I don't understand what to change and where – Valentin Gjorgoski Jun 15 '19 at 07:20
  • Can you tell from where you are launching your fragment ' – Rajat Kumar Tyagi Jun 15 '19 at 07:31
  • In MainActivity I have setContentView(R.layout.activity_main); and I call the class in activity_main.xml with and in MainFragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false); } I don't know it is right way I know a little for android – Valentin Gjorgoski Jun 15 '19 at 07:39
  • First replace your intent Intent myIntent = new Intent(view.getContext(), MainActivity.class); myIntent.putString("data",value); startActivityForResult(myIntent, 0); – Rajat Kumar Tyagi Jun 15 '19 at 07:51