开发者

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 witho开发者_Python百科ut knowing its name?

Here's the index loop:

Field[] fLst = R.string.class.getFields();

for(Field f : fLst){

  Log.i(dbgTag, "Field Entry: R.string." + f.getName());

}

Thanks for your efforts ...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜