androidgetstring

Android getString issue


So I have many strings in strings.xml, they are recorded in a format of:

<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......

Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:

for (int i = 1; i <= num; i++) {

    // Just showing what I mean.
    String xxx = getString(R.string.("list_" + i)); 

}

Is there a method to do so?


Solution

  • Try this:

    int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
    
    String xxx = getString(resourceID);