通过Android的字符串资源索引 [英] Indexing through Android string resources
问题描述
OK,我已经得到了code,让我通过指数的字符串资源。现在,我怎么得到一个特定的资源项目的价值不知道它叫什么名字?
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?
下面是该指数环:
Field[] fLst = R.string.class.getFields();
for(Field f : fLst){
Log.i(dbgTag, "Field Entry: R.string." + f.getName());
}
感谢你的努力...
Thanks for your efforts ...
推荐答案
首先,你知道资源的名称。你甚至把它在你的登录
电话。因此,一种选择是使用则getIdentifier()
在资源
对象(通常通过调用获得 getResources()
在活动
或其他上下文
)。
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
).
或者,如果您有字段
对象,调用 f.getInt(R.string.class)
Or, given that you have the Field
object, call f.getInt(R.string.class)
.
在这两种情况下(则getIdentifier()
或调用getInt()
),你现在的数字标识符资源,此时您可以拨打的getString()
在活动
来获得实际的字符串
值。
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.
使用反射 - 无论是直接或通过则getIdentifier()
- 将是缓慢的。请避免你想尽可能的办法。如果你绝对要使用反射,一定要缓存结果,所以你不需要重复做同样的查询。
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.
这篇关于通过Android的字符串资源索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!