2016-01-20 48 views
0

我有一张桌子,其中我有一列,其中图片名称像R.drawable.pic101,R.drawable.pic2已保存。现在从数据库中检索im在StringArrayList中存储这些名称。现在到setImageResource这些名字应该在IntegerArrayList ..现在我需要将StringArrayList转换为IntegerArrayList。请给我一个可能的解决方案。无法分配Setimage资源整数

+0

请出示您的代码..... – Opiatefuchs

+0

我adaoptor代码 –

+0

这里有关于如何将字符串转换为可绘制资源一看--- http://stackoverflow.com/questions/4313007/setting- android-images-from-string-value – Tasos

回答

0

你可以用资源ID ArrayList中保存为一个整数,以获得与字符串名称,你需要获得其标识的资源:

private Integer getResourceByString(Context context, String name) { 
    return context.getResources().getIdentifier(name, "drawable", context.getPackageName()); 
} 

或者你也可以直接设置所需的可绘制到ImageView的:

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