I have this method, and I want to use getResources() inside it:
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.itemTitle.setText(titles[i]);
viewHolder.itemImage.setImageResource(images[i]);
viewHolder.itemImage.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.item_image, 200, 200));
}
onBindViewHolder (VH holder, int position) Called by RecyclerView to display the data at the specified position. void. onBindViewHolder (VH holder, int position, List<Object> payloads) Called by RecyclerView to display the data at the specified position. Why is RecyclerView called RecyclerView?
However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable. What is the adapter responsible for?
It is called by RecyclerView to display the data at the specified position. This method is used to update the contents of the itemView to reflect the item at the given position. Thanks for contributing an answer to Stack Overflow!
The method getResource () returns a URL for the resource. If the resource does not exist or is not visible due to security considerations, the methods return null. The getResource () and getResourceAsStream () methods find a resource with a given name. They return null if they do not find a resource with the specified name.
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Resources res = viewHolder.itemView.getContext().getResources();
...
}
i can't use getResources inside onBindViewHolder()?
yes, you can. Use
viewHolder.itemView.getResources();
getResources()
is part View. No need to pass Context
around
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With