Issue
I have 2 activities, one with buttons which have ID's. Depending on which button you click, an ID is passed to a second activity through it's intent. I then in that second activity want to retrieve string values based on the id of that button. I had named my string values as followed:
<resources>
<string name="app_name">DataApp</string>
<string name="info_title_zalm">Zalm</string>
<string name="info_weight_zalm">1kg</string>
<string name="info_origin_zalm">Atlantische Oceaan</string>
<string name="info_title_haai">Haai</string>
<string name="info_weight_haai">200kg</string>
<string name="info_origin_haai">Atlantische Oceaan, zoet water uitzonderingen</string>
</resources>
So the string have a shared start of the name, for example info_title_zalm and info_title_haai. When clicking the haai button a "haai" string is passed to the second activity, where I was planning to do something such as "info_title_" + string to get the corresponding string. But because of needing to retrieve it as R.string.info_title_haai this doesn't work.
Is such a thing possible in Android?
Using Android Studio, building for Jellybean and above.
Solution
I use this:
int resId = getResources().getIdentifier("info_title_" + string, "string", getPackageName());
String s = getString(resId);
Here you have more info
Answered By - Jofre Mateu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.