1

我正在开发一个从服务器接收有关事件的数据的 android 应用程序。

在 ListView 中,我放置了事件的名称和类别以及一张图片。我的问题是:每个类别都应该有一张特定的图片。如何选择应该显示drawable文件夹中的哪些图片?

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);
4

1 回答 1

2

你可以做

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

文件名的第一部分是可绘制的 id。

于 2010-07-01T16:39:49.060 回答