2016-01-20 52 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); 
}