Abhishek Honrao
Abhishek Honrao

Reputation: 35

How to pass a variable from adapter to fragment?

I have a recyclerview. Once user clicked on any item of recyclerview item it should open a new fragment. But I would like to pass a id with it like we send with intent using putExtra.

Below is my adapter onBindViewHolder method code -

public void onBindViewHolder(@NonNull DashboardViewHolder holder, int position) {
        final Dashboard product = dashboardList.get(position);

        //loading the image
        Glide.with(mCtx)
                .load(product.getImage())
                .into(holder.imageView);

        holder.rate.setText(product.getRate());
        holder.name.setText(product.getName());
        holder.city.setText(product.getCity());
        //holder.id.setText(String.valueOf(product.getId()));
        holder.boatList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mCtx, ""+String.valueOf(product.getId()), Toast.LENGTH_SHORT).show();
                //Intent intent = new Intent(mCtx, AddNewBoatFragment.class);
                //intent.putExtra("boat_id", product.getId());
                //intent.putExtra("owner_id", product.getOwner_id());
                //mCtx.startActivity(intent);
                //getSupportFragmentManager().beginTransaction().replace(R.id.dahsboard_fragment,
                        //new MyBoatFragment()).commit();
                Fragment fragment = new AddNewBoatFragment();
                FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.dahsboard_fragment, fragment);
                ft.commit();
            }
        });
    }

Below is my class -

public Dashboard(int id, int owner_id, String image, String rate, String name, String city){
        this.id = id;
        this.owner_id = owner_id;
        this.image = image;
        this.rate = rate;
        this.name = name;
        this.city = city;
    }

    public int getId(){ return id; }

    public String getImage() {
        return image;
    }

    public String getRate() { return rate; }

    public String getName(){
        return name;
    }

    public String getCity() { return city; }

    public int getOwner_id() { return owner_id; }

Upvotes: 3

Views: 3387

Answers (3)

Son Truong
Son Truong

Reputation: 14173

You should declare a static method in AddNewBoatFragment to create a Fragment instance from given params.

public class AddNewBoatFragment extends Fragment {

    private static final String ARGUMENT_BOAT_ID = "ARGUMENT_BOAT_ID";
    private static final String ARGUMENT_OWNER_ID = "ARGUMENT_OWNER_ID";

    public static AddNewBoatFragment newInstance(int boatId, String ownerId) {
        Bundle args = new Bundle();
        // Save data here
        args.putInt(ARGUMENT_BOAT_ID, boatId);
        args.putString(ARGUMENT_OWNER_ID, ownerId);
        AddNewBoatFragment fragment = new AddNewBoatFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        // Retrieve the data here
        int boatId = getArguments().getInt(ARGUMENT_BOAT_ID);
        String ownerId = getArguments().getString(ARGUMENT_OWNER_ID);
        return view;
    }
}

And modify block code in onClick method from the Adapter class.

holder.boatList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = AddNewBoatFragment.newInstance(product.getId(), product.getOwner_id());
        FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.dahsboard_fragment, fragment);
        ft.commit();
    }
});

Upvotes: 2

Nandu
Nandu

Reputation: 63

I just put those variables which need to pass to a fragment as Public Static which can be accessed from any other class

Upvotes: 0

Zaid Mirza
Zaid Mirza

Reputation: 3699

You can send using Bundle

        Fragment fragment = new AddNewBoatFragment();
        Bundle bundle = new Bundle();
        bundle.putString("myIDKey",product.getId());
        fragment.setArguments(bundle);
        FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.dahsboard_fragment, fragment);
        ft.commit();

and in OnCreateView method of Fragment you can get attached arguments and use it

Upvotes: 0

Related Questions