Skip to content Skip to sidebar Skip to footer

How To Access An OnBindViewHolder Item Value From MainActivity In Android

I have an OnbindViewHolder method in my CarDetailsListAdapter.java file like below @Override public void onBindViewHolder(final CarDetailsListAdapter.ViewHolder viewHolder, final

Solution 1:

You want to access car_amount but you have multiple cell in your recyclerview. I will suppose that you want to access it after an "onClick" on your cell.

You can give to your adapter a listener to call when a click is done on a specific cell.

public CarDetailsListAdapter(CarDetailsListListener listener){
    mListener = listener;
}

@Override
public void onBindViewHolder(final CarDetailsListAdapter.ViewHolder viewHolder, final int i) {
   viewHolder.itemView.setOnClickListener(v->{
      mListener.onClick(currentItem.getCarAmount());
   }
}

Your activity :

public CarDetailsListActivity extends Activity implements CarDetailsListListener {
   ...

   mAdapter = new CarDetailsListAdapter(this);

   @Override
   public void onClick(int carAmount){
      ...
   }
}

Post a Comment for "How To Access An OnBindViewHolder Item Value From MainActivity In Android"