1

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">
    <item>item1</item>
    <item>item2</item>
    <item>item3</item>
</string-array>

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?

JW782
  • 13
  • 2
  • 1
    It sounds like you want to look up a resource by its name dynamically, rather than hard-coding to the resource identifier? If so, check out this answer: https://stackoverflow.com/a/3476470/3032 – Scott W Sep 08 '22 at 01:39

1 Answers1

0

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);
JamesNickel
  • 303
  • 1
  • 15
  • I had to use some more code in the calling activity to pull the resource ID properly, but this works far better than what I was initially trying to do. Thanks. – JW782 Sep 13 '22 at 01:05