1

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?

8
  • you need context or activity instance to use getRestources() method. Or simply try to call getApplicationContext().getResources() if your adapter in activity Commented Nov 25, 2016 at 8:31
  • int imageId = getResources().getIdentifier("image_name", "drawable", getPackageName()); imageView.setImageDrawable(ContextCompat.getDrawable(context, iStopImageId)); Commented Nov 25, 2016 at 8:33
  • instead of using String str = "R.drawable.e0"; use int img=R.drawable.e0 Commented Nov 25, 2016 at 8:33
  • @Kintanpatel There is no issue with context, the problem is only at getResources() Commented Nov 25, 2016 at 8:35
  • @Daryl That doesn't sound feasible, as I have e0,e1,.......e20 images Commented Nov 25, 2016 at 8:36

3 Answers 3

1

You can pass the context to the Adapter in constructor. Then you can access to getResources() through that context.

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    ...

    private Context context;

    public MyAdapter(Context context, Other_arguments_you_need) {
        this.context = context;
        // set other arguments
    }

    ...

    @Override
    public void onBindViewHolder(HomeTeamViewHolder homeTeamViewHolder, int i)
    {

        homeTeamViewHolder.teamName.setText(d1.teams.get(i));
        int resourceId = context.getResources().getIdentifier("testimage", "drawable", "your.package.name");
        homeTeamViewHolder.home_teams.setImageDrawable(ContextCompat.getDrawable(homeTeamViewHolder.mcontext,resourceId));

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Fixed it by passing context to the Adapter.

 Context mcontext = itemView.getContext();

then, inside onBindViewHolder() :

@Override
public void onBindViewHolder(HomeTeamViewHolder homeTeamViewHolder, int i)
{

    homeTeamViewHolder.teamName.setText(d1.teams.get(i));
    String x = "e"+i;
    int resourceId = homeTeamViewHolder.mcontext.getResources().getIdentifier(x, "drawable", "com.udacity.yaafl");
    homeTeamViewHolder.home_teams.setImageDrawable(ContextCompat.getDrawable(homeTeamViewHolder.mcontext,resourceId));

}

Comments

0

grab the Integer ID of a resource by this

  int resourceId = homeTeamViewHolder.itemView.getResources().getIdentifier("testimage", "drawable", "your.package.name");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.