【发布时间】:2013-08-29 06:01:28
【问题描述】:
我有一个包含两列 _id 和 name 的 SQlite 表。出于国际化目的,我将名称存储为 R.string.today
现在我想获取与R.string.today关联的字符串资源:
String column = myCursor.getString(myCursor.getColumnIndex(MainDb.NAME));
int resId = getApplicationContext().getResources().getIdentifier(column, "strings", getApplicationContext().getPackageName());
String buttonText;
if (resId != 0)
{
buttonText = getApplicationContext().getString(resId);
} else
{
buttonText = "Resource is null!";
}
resId 始终为 0。变量 column 具有正确的值,我已经用调试器检查过了。我在resId 中尝试了不同的变体:
- “字符串”而不是 defType 中的“字符串”。
- getResources 的不同变体,例如 Resources.getSystem()...
- getPackageName() 的不同变体
我做错了什么?
【问题讨论】:
-
也许我没有得到你的 sn-p 但是......你为什么不直接使用
int resId = getApplicationContext().getResources().getString(R.string.MYSTRING)? -
getResources() 需要活动上下文
String s = getResources().getString(R.string.today) -
你试过只使用
today而不是R.string.today吗?