views:

38

answers:

1

I am developing an android application that receives data about events from a server.

In a ListView I put the name and category of the event and a picture. My problem is: each category should have a specific picture. How do I choose which of the pictures in the drawable folder should be displayed?

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  A: 

You can do

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

The first part of the filename is the drawable id.

BrennaSoft