How to change image of imageView from drawable inside an adapter of RecyclerView after an item has been clicked?

3343 views java
-1

  1. I fetched data from SERVER to populate RECYCLER view
  2. I have an imageview in each recyclerview row
  3. When I click that imageview, I want to load my drawable into the imageview

    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
    
            Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app);
            holder.imageView.setImageDrawable(placeholder);
    
        }
    });
    

The above is what I tried but it says "holder" needs to be declared final but in reality, I haven't even mentioned it in a way I could write "final holder" or whatever.

My ADAPTER code :

public class ProductsAdapter extends 
RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {


private Context mCtx;
private List<Product> productList;

public ProductsAdapter(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.product_list, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    final Product product = productList.get(position);

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

    holder.textViewTitle.setText(product.getTitle());
    holder.textViewShortDesc.setText(product.getShortdesc());
    holder.textViewRating.setText(String.valueOf(product.getRating()));
    holder.textViewPrice.setText(String.valueOf(product.getPrice()));


    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app);
            holder.imageView.setImageDrawable(placeholder);

        }
    });



        }

@Override
public int getItemCount() {
    return productList.size();
}

class ProductViewHolder extends RecyclerView.ViewHolder {

    TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
    ImageView imageView;

    public ProductViewHolder(View itemView) {
        super(itemView);

        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
        textViewRating = itemView.findViewById(R.id.textViewRating);
        textViewPrice = itemView.findViewById(R.id.textViewPrice);
        imageView = itemView.findViewById(R.id.imageView);
    }
}
}

answered question

2 Answers

2

You can declare the holder final in the method header

public void onBindViewHolder(final ProductViewHolder holder, int position) {
    Your code here
}

posted this
0

There are several solutions:

1) Make your holder to be final doing

@Override
public void onBindViewHolder(final ProductViewHolder holder, int position) 

or using additional variable

final Holder finalHolder = holder;
holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Drawable placeholder = finalHolder.imageView.getContext().getResources().getDrawable(R.drawable.app);
            holder.imageView.setImageDrawable(placeholder);

        }
    });

2) When you set ClickListener on particular view, you are sire that only that view will trigger action, so you can use that knowledge

holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ImageView imgView = (ImageView) view;
            Drawable placeholder = imgView.getContext().getResources().getDrawable(R.drawable.app);
            imgView.setImageDrawable(placeholder);

        }
    });

posted this

Have an answer?

JD

Please login first before posting an answer.