0

I know there are duplicate questions for this. But their problem is between Activity and Fragment . My problem is about passing a value from Adapter to Fragment.

I Toast the value from adapter and it shows the correct value. I used the putString and setArguments methods but it returns null.

Here is what I did in my Adapter:

 Bundle cart_bundle = new Bundle();
        cart_bundle.putString("total", String.valueOf(total_amount));
        Cart_Fragment fragment = new Cart_Fragment();
        fragment.setArguments(cart_bundle);
        ((User_Activity)context).getSupportFragmentManager().beginTransaction()
                .commit();

Here is how I receive the argument in Fragment :

Bundle cart_bundle = this.getArguments();
tv_total_amount.setText("Total Amount: "+ cart_bundle.getString("total"));

Maybe there is something wrong in ((User_Activity)context).getSupportFragmentManager().beginTransaction() .commit();

There is an error if I try to replace the User_Activity to Fragment.

P.S. I don't need to use replace() method because the adapter is an adapter for the list view in the fragment.

3 Answers3

1

The best and good way to pass the adapter is first you have pass the object of the class from where you create the adapter . suppose :-

Adapter adapter=new Adapter(activityname or fragmentname.class);

here on click of item In adapter class;-

adapter.updateData(xyz,abz)

will create the method in that Fragment.

IN Fragment , you have to make interface that will update the data or you can directly call fragment , here you get the how to pass data How to pass values between Fragments

or you can

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("total", String.valueOf(total_amount));
fragment.setArguments(bundle);
Praveen Rawat
  • 314
  • 1
  • 3
  • 14
0

The way you are getting the argument is wrong. Fragment has setArgument method which you need to override in your code and one your load your fragment you can call fragment instance and setargument.

So your fragment class will look like

public class Cart_Fragment extends Fragment{
    public void setArguments(Bundle args) {
         super.setArguments(args);
         //Get the argument here.
    }
}

Modify your adapter code as

Cart_Fragment fragment = new Cart_Fragment();
((User_Activity)context).getSupportFragmentManager().beginTransaction().commit();

Bundle cart_bundle = new Bundle();
cart_bundle.putString("total", String.valueOf(total_amount));
fragment.setArguments(cart_bundle);
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0

You are creating a new instance of Cart_Fragment, setting the new arguments to it but never using it. The instance being used (the one that was actually transacted to replace or add) and this new instance of the Fragment are not the same. Hence, you will not get the arguments in the existing Fragment magically.

What you can do instead is keep a reference of the Fragment you first transacted and pass the values to it using a custom method of your own like:

public class Cart_Fragment extends Fragment {
    public void setNewTotal(String total) {
         tv_total_amount.setText("Total Amount: " + total);
    }
}

And in your adapter:

fragment.setNewTotal(String.valueOf(total_amount));

If you have used the Fragment right in the XML like:

<fragment
    android:id="@+id/cart_fragment"
    android:name="com.example.Cart_Fragment"
    ...

you can get the instance of the fragment with:

Cart_Fragment fragment = ((Cart_Fragment) getSupportFragmentManager().findFragmentById(R.id.cart_fragment));
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55