[英]Cannot assign Setimage resource Integer
我有一张桌子,其中有一列保存图片名称,如R.drawable.pic101
, R.drawable.pic2
。 现在从数据库im中检索将这些名称存储在String
的ArrayList
中。 我们setImageResource
这些名字应该是Integer
ArrayList
..现在我需要转换ArrayList
的String
到ArrayList
的Integer
。 请给我一个可能的解决方案。
您可以将具有资源ID的ArrayList保存为整数,以获取具有字符串名称的资源,您需要获取其标识符:
private Integer getResourceByString(Context context, String name) {
return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}
或者,您也可以直接将所需的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.