Skip to content Skip to sidebar Skip to footer

Indexing Through Android String Resources

OK, I've got the code to allow me to index through the string resources. Now, how do I get the value of a specific resource item without knowing its name? Here's the index loop: Fi

Solution 1:

First, you know the resource's name. You are even putting it in your Log call. So, one option would be to use getIdentifier() on a Resources object (usually obtained by calling getResources() on your Activity or other Context).

Or, given that you have the Field object, call f.getInt(R.string.class).

In either case (getIdentifier() or getInt()), you now have the numeric identifier of the resource, at which point you can call getString() on your Activity to get the actual String value.

Using reflection -- whether directly or via getIdentifier() -- is going to be slow. Please avoid the approach you are trying where possible. If you absolutely have to use reflection, be sure to cache your results, so you do not need to do the same lookups repeatedly.

Post a Comment for "Indexing Through Android String Resources"