2010-07-01 90 views
0

我正在開發一個android應用程序,它接收有關來自服務器的事件的數據。選擇可繪製文件路徑

在ListView中,我把事件和圖片的名稱和類別。 我的問題是:每個類別應該有一個特定的圖片。如何選擇可繪製文件夾中的哪些圖片應顯示?

View v; /**I get this view from the parameters.**/ 
ImageView iconCategory = (ImageView) v .findViewById(R.id.category_icon); 
Drawable drawable = null; 
if (o.getCategoria().equals("category1")) { 
/* here I want to set the icon to the file named icon_category1.png . I wanna do that by instantiating drawable */ 
} else if (o.getCategoria().equals("category2")) { 
/* here I want to set the icon to the file named icon_category2.png I wanna do that by instantiating drawable */ 
} else if (o.getCategoria().equals("category3")) { 
/* here I want to set the icon to the file named icon_category3.png I wanna do that by instantiating drawable */ 
} 
iconCategory.setImageDrawable(drawable); 

回答

2

你可以做

Drawable d = getResources().getDrawable(R.drawable.icon_category2); 
iconCategory.setImageDrawable(d); 

文件名的第一部分是可繪製的ID。