As mentioned in the title, I have a RecyclerView inside which I need to populate 20 images. Since the images are available in drawable I am trying to programmatically set these images from the adapter of RecyclerView.
@Override
public void onBindViewHolder(HomeTeamViewHolder homeTeamViewHolder, int i)
{
homeTeamViewHolder.teamName.setText(d1.teams.get(i));
String str = "R.drawable.e0";
homeTeamViewHolder.home_teams.setImageDrawable(ContextCompat.getDrawable(homeTeamViewHolder.mcontext,str));
}
Now, this obviously doesn't work as Android studio says:
Wrong 2nd argument type. Found: 'java.lang.String', required: 'int'
For which, a solution already exists: Setting Android images from string value
However, when I tried:
int resourceId = Activity.getResources().getIdentifier("testimage", "drawable", "your.package.name");
inside onBindViewHolder() :
@Override
public void onBindViewHolder(HomeTeamViewHolder homeTeamViewHolder, int i)
{
homeTeamViewHolder.teamName.setText(d1.teams.get(i));
//String str = "R.drawable.e0";
int resourceId = Activity.getResources().getIdentifier("testimage", "drawable", "your.package.name");
homeTeamViewHolder.home_teams.setImageDrawable(ContextCompat.getDrawable(homeTeamViewHolder.mcontext,resourceId));
}
Android Studio says:
Non-static method
getResources()cannot be referenced from a static context
So, the question is: how to fix this?
getResources()