Accesing to a drawable through its name in Android

送分小仙女□ 提交于 2019-12-12 03:44:31

问题


I'm developing an app thant handles a SQLite database. I just get some information from it and show it in a list of CardViews. Each CardView basically displays the name, the age and a picture of a person.

Everything is working fine, but now that I have implemented the database, I have some doubts about it.

I just need to access to the drawables through the name of the files. So far I've been working manually with the R.drawable id, so I could check if it was working. Just like this:

Person p1 = new Person(name, age, R.drawable.person1);

And then I just add that Person to a List of Persons, to then pass this List as a parameter to the Adapter.

Basicaly, the name of the person and the name of the image is exactly the same. So, now that I get all of the persons from the database (in a list), I'd like to get the drawable from the file name, but I only now how to get it through the R.drawable.id of the picture.

I'd like to change the R.drawable attribute from an int to a String, so I can access the way I want.

My question is, is there anyway to do something like getDrawable("name_of_the_file.png"); or anything similar? Or is there any way to get the R.drawable.id through the name of the file?

Thanks in advance.


回答1:


is there anyway to do something like getDrawable("name_of_the_file.png"); or anything similar?

No.

Or is there any way to get the R.drawable.id through the name of the file?

Call getIdentifier() on a Resources object to retrieve the numeric R value given the package, resource type (e.g., "drawable"), and resource name (e.g., "example").



来源:https://stackoverflow.com/questions/38447550/accesing-to-a-drawable-through-its-name-in-android

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