2012-03-09 6 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); 

내가 생각할 수있는 유일한 점은 패키지 이름에 문제가 있지만 괜찮은 것 같습니다.

어떤 도움이 필요합니까?

답변

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()); 
관련 문제