2012-03-09 93 views
1

我已經看到了關於這個問題的幾個線程,但我不知道他們爲什麼不爲我工作。這裏的短代碼:通過名稱獲取字符串資源

//This doesn't work. I'm getting a 0 id 
id=getResources().getIdentifier("q1", "id", getPackageName()); 

//This retrieves the string correctly (just to prove the string is called q1 
String str=getResources().getString(R.string.q1); 

我能想到的唯一的問題是packagename的問題,但它似乎確定。

任何幫助?

回答

0

獲取資源方式名稱

String packageName = "YOUR_PACKAGE_NAME"; 
String abString="Resources_Name"; 
int resId = getResources().getIdentifier(abString, "string", packageName); 
//OR 
String aString="Resources_Name"; 
int resId = getResources().getIdentifier(aString, null, null); 
1

是否應該如下?

id=getResources().getIdentifier("q1", "string", getPackageName()); 
相關問題