[英]Cannot assign Setimage resource Integer
我有一張桌子,其中有一列保存圖片名稱,如R.drawable.pic101
, R.drawable.pic2
。 現在從數據庫im中檢索將這些名稱存儲在String
的ArrayList
中。 我們setImageResource
這些名字應該是Integer
ArrayList
..現在我需要轉換ArrayList
的String
到ArrayList
的Integer
。 請給我一個可能的解決方案。
您可以將具有資源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.