Obtener recurso de cadena por nombre

I have seen several threads about this question but I don't know why they are not working for me. Here the short code:

//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);

The only thing I could think about is a problem with the packagename, but it seems Ok.

¿Alguna ayuda?

preguntado el 09 de marzo de 12 a las 16:03

2 Respuestas

Should it be the following?

 id=getResources().getIdentifier("q1", "string", getPackageName());

respondido 09 mar '12, 16:03

Get Resource By Name

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);

respondido 09 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.