选择可绘制文件路径

时间:2010-07-01 16:36:15

标签: android drawable

我正在开发一个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);

1 个答案:

答案 0 :(得分:2)

你可以做到

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

文件名的第一部分是drawable id。