While using recycler views, I just want to pass the string values of resource id's of the images, but I know that it can't be done using the usual way, like we cannot pass a string in setImageResource()
. But is there a way to convert the R.id.anyImage
into its integer value?
Asked
Active
Viewed 757 times
0

Arpit Anand
- 347
- 2
- 17
-
what do you mean by passing string values? do you have string value like the name of the image? – akhilesh0707 Sep 10 '20 at 18:59
-
if you have a resource name as a string you can get the resource id using `getIdentifier` – akhilesh0707 Sep 10 '20 at 19:02
-
Duplicates: https://stackoverflow.com/q/5254100, https://stackoverflow.com/q/4313007, https://stackoverflow.com/q/11737607, https://stackoverflow.com/q/13351003, https://stackoverflow.com/q/2414134. – Mike M. Sep 10 '20 at 19:06
1 Answers
1
Unfortunately, there is no getImageResource() or getDrawableId(). But, You can create simple workaround by using the ImageView tags.
In onCreate():
imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);
Then, if you like, you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv)
{
return (Integer) iv.getTag();
}
Too easy.

TuitionAppSpl
- 146
- 1
- 8