Issue
I have a String that Fetched from a XML file named greece_ship. and also i have an image file in res/drawable folder in my project. i wanna set an ImageView that get it's resource file from my greece_ship string. i tried this but it's not work:
String image = "R.drawable." + tour.getImage();
int res = Resources.getSystem().getIdentifier(image, "drawable", String.valueOf(Drawable.class));
vPic.setImageResource(res);
my next try is this String image = tour.getImage(); in line 1 Instead of String image = "R.drawable." + tour.getImage();, but it's not work yet. help me please. Thank you very much.
Solution
greece_ship.png is probably not part of android, but it is something you added to the res/drawable, so you should use getResources().getIdentifier , and not Resources.getSystem().getIdentifier. The first parameter is the drawable's name. Just the name, and your case is the result of tour.getImage(), without prepending anything. The third parameter is the package name. You can get it using getPackageName(). In the end it should look like:
int res = getResources().getIdentifier(tour.getImage(), "drawable", getPackageName());
assuming that you have a context to access the resources and tour.getImage() returns the drawable's name without extension
Answered By - Blackbelt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.