这个函数siply调用一个从对象传递id的方法:
public void onClick(View v) {
showDesc("cgpc_1c");
}
该方法必须在strings.xml中获取字符串的值并将其传递给活动:
private void showDesc(String id){
String path = "R.string." + id;
String id_desc = getResources().getString(path);
final Intent intent = new Intent(Cgpc.this, Description.class);
Bundle b = new Bundle();
b.putString("ID_DESC", id_desc);
startActivity(intent);
}
但是...这不喜欢Java,告诉我不是字符串作为路径,而是需要一个整数......我该如何解决这个问题?
谢谢!
答案 0 :(得分:2)
尝试使用getIdentifier:
1 - 添加此方法
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
2 - 这样称呼:
getResourceID("myStringName", "string", getApplicationContext());