简体   繁体   中英

Cannot assign Setimage resource Integer

I have a table in which i have one column where pictures names like R.drawable.pic101 , R.drawable.pic2 are saved. Now retrieving from database im storing theses names in an ArrayList of String . Now to setImageResource these names should be in Integer ArrayList .. Now i need to convert ArrayList of String to ArrayList of Integer . Please give me a possible solution.

You can save your ArrayList with resources ID as an integer, to get resources with string names you need to obtain its identifiers:

private Integer getResourceByString(Context context, String name) {
    return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}

Or you could also set directly the desired Drawable to the ImageView:

private void setStringResourceImageView(ImageView imageView, String name) {
    Context context = imageView.getContext();
    Integer resId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
    imageView.setImageResource(resId);
}

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