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 に答える