简体   繁体   中英

How to set image programmatically to an ImageView inside an adapter using String value?

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?

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));

    }
}

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));

}

以此抓取资源的整数ID

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM