由网友(断桥烟雨ベ旧人殇)分享简介:有什么不对的code?我想自动使用ID。我认为R.string后有一个错误。我能做些什么What's wrong with this code? I want to use id automatically. I think after R.string there is a mistake. What can ı...
有什么不对的code?我想自动使用ID。我认为R.string后有一个错误。我能做些什么
What's wrong with this code? I want to use id automatically. I think after R.string there is a mistake. What can ı do
推荐答案
做这样
public static int getStringIDFromName(String stringName)
{
int stringID= 0;
if(stringName == null
|| stringName.equalsIgnoreCase(""))
{
return 0;
}
try
{
@SuppressWarnings("rawtypes")
Class res = R.string.class;
Field field = res.getField(stringName);
stringID = field.getInt(null);
}
catch(Exception e)
{
// Error
}
return stringID;
}
设置你的价值是这样
Set your value like this
int stringVal = getStringIDFromName("i" + j++);
if( stringVal != 0)
txtt.setText(getResource().getString(stringVal));
如果你正在做的一切好的,这只会工作。
This would work only if you are doing everything else right.
相关推荐
最新文章