繁体   English   中英

无法分配Setimage资源整数

[英]Cannot assign Setimage resource Integer

我有一张桌子,其中有一列保存图片名称,如R.drawable.pic101R.drawable.pic2 现在从数据库im中检索将这些名称存储在StringArrayList中。 我们setImageResource这些名字应该是Integer ArrayList ..现在我需要转换ArrayListStringArrayListInteger 请给我一个可能的解决方案。

您可以将具有资源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.

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