Pull String Array Dynamically with name


I want to use an intent string extra to dynamically determine what String array in an XML file to use.
Java code:

 Intent myIntent = getIntent();
 String stringID = myIntent.getStringExtra("stringID");//pull string id

 String[] allStrings = getResources().getStringArray(stringID);

The XML:

<string-array name="set1">

The last line in the java code doesn’t work because it wants something like r.array.set1, but I want to choose this dynamically instead. How can I accomplish this? Would it be easier to use the ID of the string array somehow?


Actually r.array.set1 is a reference to an Integer. So you should pass it as an Integer, not a String. So:

Intent myIntent = getIntent();
int intID = myIntent.getIntExtra("stringID", r.array.set1);//The second parameter is the default value if nothing is specified

String[] allStrings = getResources().getStringArray(intID);

And in the other activity pass it as an Integer:

intent.putExtra("stringID", r.array.set1);

Answered By – JamesNickel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published