问题
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