简体   繁体   English

无法分配Setimage资源整数

[英]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. 我有一张桌子,其中有一列保存图片名称,如R.drawable.pic101R.drawable.pic2 Now retrieving from database im storing theses names in an ArrayList of String . 现在从数据库im中检索将这些名称存储在StringArrayList中。 Now to setImageResource these names should be in Integer ArrayList .. Now i need to convert ArrayList of String to ArrayList of Integer . 我们setImageResource这些名字应该是Integer ArrayList ..现在我需要转换ArrayListStringArrayListInteger 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: 您可以将具有资源ID的ArrayList保存为整数,以获取具有字符串名称的资源,您需要获取其标识符:

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: 或者,您也可以直接将所需的Drawable设置为ImageView:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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