Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use getResources inside of onBindViewHolder()?

Tags:

android

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));
    }
like image 330
Ahmed Safwat Ewida Avatar asked Feb 26 '17 11:02

Ahmed Safwat Ewida


People also ask

What is onbindviewholder in recyclerview?

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?

What is the difference between onbindviewholder and setonclicklistener?

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?

What is the use of itemview method in recyclerview?

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!

What is the difference between getResource () and getresourceasstream () methods?

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.


2 Answers

public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Resources res = viewHolder.itemView.getContext().getResources();
    ...
}
like image 155
Nick Cardoso Avatar answered Oct 21 '22 10:10

Nick Cardoso


i can't use getResources inside onBindViewHolder()?

yes, you can. Use

   viewHolder.itemView.getResources();

getResources() is part View. No need to pass Context around

like image 38
Blackbelt Avatar answered Oct 21 '22 09:10

Blackbelt