Select Drawable file path

独自空忆成欢 提交于 2019-12-11 09:46:27

问题


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

回答1:


You can do

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

The first part of the filename is the drawable id.



来源:https://stackoverflow.com/questions/3159793/select-drawable-file-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!