2

I have a list of items. These data is received from a API and then listed trough a recycle view. This is how it looks now: https://i.stack.imgur.com/NY34j.jpg

When I press the heart on one of those items, I want to receive the data from that specific item. I'm not sure how do this. Right now I have created a listener to the heart icon, but now I'm stuck. I want to get the data which includes the name, the pictureURL and the expire date.

I have a adapter in which I bind my data with the view. Here is the onCreateViewHolder:

@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //inflating and returning our view holder
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.layout_food, parent, false);
    return new FoodViewHolder(view);
}

This is my OnBindViewHolder-method:

@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
    //getting the product of the specified position
    Food food = foodList.get(position);

    holder.heart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v){
            switch (v.getId()) {
                case R.id.heart:
//what code do i write here???
break;
            }
        }
    });

Is this the correct way to do it? Where do I go from here?

user7388204
  • 57
  • 1
  • 7

3 Answers3

2

To get item inside listeners, you can use getAdapterPosition of your ViewHolder:

Food clickedFood = foodList.get(holder.getAdapterPosition());

See docs for more: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getAdapterPosition()

Then, you can access fields and methods of the clickedFood to get specific data pies.

S-Sh
  • 3,564
  • 3
  • 15
  • 19
0

Create a interface to send data to activity In Adapter class:

private FoodListener foodListener;

public void setClickItemListener(FoodListener foodListener) {
    this.foodListener= foodListener;
}
public interface FoodListener {
    void onHeartClick(Food food);
}


@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
      //getting the product of the specified position
      final Food food = foodList.get(position);

      holder.heart.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v){
        switch (v.getId()) {
            case R.id.heart:
                foodListener.onHeartClick(food);
        }
     }
});

Implement interface in activity:

public class MainActivity implements Adapter.FoodListener {
    @Override
    onCreate(...) {
          .....
          recyclerViewAdapter.setListener(this);
    }
    @Override
    void onHeartClick(Food food) {
         //Get food here
    }
}
Tung Tran
  • 2,885
  • 2
  • 17
  • 24
0

You need to set click listener on the view in your OnCreateViewHolder method like this and pass the data to interface method so you can update your Activity. Hope this helps.

 @Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //inflating and returning our view holder
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.layout_food, parent, false);
     FoodViewHolder holder = new FoodViewHolder(view);
    holder.heartView.setOnClickListener { v-> 
              Food food = list.get(holder.getAdapterPosition());
        foodListener.OnClick(food);
    }
    return holder;
}
Mobin Munir
  • 9
  • 1
  • 1
  • 4