Java 无法分配Setimage资源整数

Java 无法分配Setimage资源整数,java,android,android-imageview,Java,Android,Android Imageview,我有一个表,其中有一列保存了图片名称,如R.drawable.pic101,R.drawable.pic2。现在从数据库中检索,我将这些名称存储在String的ArrayList中。现在转到setImageResource这些名称应该在IntegerArrayList中。。现在我需要将String的ArrayList转换为Integer的ArrayList。请给我一个可能的解决方案。您可以将带有资源ID的ArrayList保存为整数,以获取带有字符串名称的资源,您需要获取其标识符: privat

我有一个表,其中有一列保存了图片名称,如
R.drawable.pic101
R.drawable.pic2
。现在从数据库中检索,我将这些名称存储在
String
ArrayList
中。现在转到
setImageResource
这些名称应该在
Integer
ArrayList
中。。现在我需要将
String的
ArrayList
转换为
Integer的
ArrayList
。请给我一个可能的解决方案。

您可以将带有资源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);
}

请显示您的代码…..在此查看如何将字符串转换为可绘制资源---BestListAdapter=new BestListAdapter(FoodItem.this,images,names,calory,details,quan,chek1);lists=(ListView)findViewById(R.id.fooditemlist);lists.setAdapter(适配器);别忘了从数据库条目中删除所有的(R.drawable),因为你不再需要它们了,所以只需存储pic名称即可