R.string; get string from dynamic key name [duplicate]
Asked Answered
N

2

15

Possible Duplicate:
Dynamic Resource Loading Android

In Android, I can load a string from the resources with String s = getString(R.string.keyName). But I have a list of categories in my database, each one which has a name. How can I take that category name and then load the appropriate string resource based on it, so that it will work for localization?

Basically, I need to have the keyName be dynamic; my own String variable. Is this possible? Thanks.

Nace answered 27/5, 2011 at 21:29 Comment(3)
the localization part is done with a lookup in say res.values (default), res.values-en-rCA (Canada) res.values-en-rGB (United Kingdom) etc. If the string is not present in the localized language for the user, it is fetched from res.values.Corena
try this: String value = view.getResources().getString(R.string.class.getField("stringName").getInt(null));Bizarre
int asd = getResources().getIdentifier("MYDYNAMICKEY","string",mContext.getPackageName()); String bla = getString(asd);Isodimorphism
T
11

As your resources cannot be dynamically, you could use a switch statement like:

String name = "";
switch (row.getNameKey()) {
case keyName1:
    name = getString(R.string.keyName1);
    break;
case keyName2:
    name = ...
    ...
}

Another approach woould be the getIdentifier method: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

and see: Android: Accessing string.xml using variable name

Tristis answered 27/5, 2011 at 21:34 Comment(9)
How is that giant switch statement different than name = getString(row.getNameKey())?Thermosphere
I'm doing that in one place right now; a place that only has 4 options. But the categories are dynamic and database-driven; there could be hundreds of values. Of course I'd still have to list them all out in the resource file, but I figure there must be some way to not have to switch it in my code like that...Nace
@Thermosphere - getNameKey()? That sounds like what I would want; but I can't find a method like that....Nace
@Sam: I edited it like it was ment to be not the String id in the case statement. @Gendolkari: getNameKey is YOUR method where you get the key from the db. So just save the strings-id in db. But this is a mess once if you change the ids.Tristis
I added another idea to my answer. Maybe this is what you are looking for?Tristis
Thanks, the getIdentifier was exactly what I was looking for!Nace
Thanks. You pointed in the right direction with the first link. Here's a blog link for the code I found steven.bitsetters.com/2007/11/27/…Upbeat
what about if i have 100+ rows in my List? i can't write Switch with 100+ casesTowroy
Thank you i get solution for the links you providedTowroy
F
4

You can use Java Reflection to turn the string into the resource ID. If you know in advance that it's a string, say R.string.theName, and you have a keyname of "theName", you just need to use reflection on "your.package.com.R.string" (where "your.package.com" is the package name defined in AndroidManifest.xml) to find the class, then use reflection to get the "theName" static member from it. The value you receive can be passed into the getString() method.

Farver answered 28/5, 2011 at 1:31 Comment(4)
This is the best answer, but it will be highly inefficient. You can find numerous samples explaining reflection in Java. Just be very careful with how often you make use of this method.Thermosphere
This isn't the best answer, as the Android SDK has methods specifically for looking up resources via name - no need to use reflection.Acrocarpous
@Acrocarpous please be specific in your answer - which methods do you mean?Reduplicate
Hi @LeosLiterak! The top answer on this page by "Stuck" briefly mentions the answer - it's getidentifier. There's example code if you follow the link at the top of this page where it says "Possible Duplicate: Dynamic Resource Loading Android". Tl;dr: Resources#getIdentifierAcrocarpous

© 2022 - 2024 — McMap. All rights reserved.