无法分配 Setimage 资源整数
Cannot assign Setimage resource Integer
我有一个 table,其中有一列保存了像 R.drawable.pic101
、R.drawable.pic2
这样的图片名称。现在从数据库中检索我将这些名称存储在 String
的 ArrayList
中。现在 setImageResource
这些名称应该在 Integer
ArrayList
.. 现在我需要将 ArrayList
of String
转换为 ArrayList
of [=15] =].请给我一个可能的解决方案。
您可以将带有资源 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);
}
我有一个 table,其中有一列保存了像 R.drawable.pic101
、R.drawable.pic2
这样的图片名称。现在从数据库中检索我将这些名称存储在 String
的 ArrayList
中。现在 setImageResource
这些名称应该在 Integer
ArrayList
.. 现在我需要将 ArrayList
of String
转换为 ArrayList
of [=15] =].请给我一个可能的解决方案。
您可以将带有资源 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);
}