R.string;从动态密钥名得到的字符串 [英] R.string; get string from dynamic key name

查看:315
本文介绍了R.string;从动态密钥名得到的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  动态资源加载的Andr​​oid

在Android中,我可以加载的资源与字符串的String =的getString(R.string.keyName)。但我有我的数据库类别的列表,其中有一个名字各一个。我怎么能采取的类别名称,然后加载基于它相应的字符串资源,使之本地化工作?

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?

基本上,我需要有的keyName 是动态的;我自己的字符串变量。这可能吗?谢谢你。

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

推荐答案

当你的资源,不能动态,你可以使用switch语句,如:

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 = ...
    ...
}

另一种方法woould是则getIdentifier方法:<一href=\"http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29\">http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

和看到:<一href=\"http://stackoverflow.com/questions/5980577/android-accessing-string-xml-using-variable-name\">Android:使用变量名访问string.xml

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

这篇关于R.string;从动态密钥名得到的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆