-3

How to get strings dynamically from strings.xml where

for(int=1; i<100; i++){
String podp = podp+i;
context.getString(R.string.+podp) //here I have to put podp variable
}

I got error identifier expected.

Piotr M.
  • 11
  • 7

1 Answers1

1

You can use this:

public static String getStringByIdName(Context context, String name) {
        Resources res = context.getResources();
        return res.getString(res.getIdentifier(name, "string", context.getPackageName()));
}

And call with dynamic name, which will return value inside <string id="podp">...</string> tag:

String podp = getStringByIdName(this, "podp");
nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
  • I tried with res.getIdentifier(podp, "string", null), but showed error int cannot be converted to string – Piotr M. Nov 20 '17 at 14:55
  • @PiotrM. Because you use it WRONG way. `res.getIdentifier()` will return resource id `@ResId` which is an int defined inside `R.java`, not your String. You MUST call `res.getString(resId)` to get your string from this resource id. – nhoxbypass Nov 20 '17 at 14:57
  • Then I obtain error Resources.getString(int) is not applicable. Its bullshit. I declared podp as a string. Its String podp = "podp"+i+n; – Piotr M. Nov 20 '17 at 15:04
  • @PiotrM. did it work? – nhoxbypass Nov 20 '17 at 15:24
  • yes, instead res.getString I used context.getString(.....); – Piotr M. Nov 20 '17 at 16:07