0

I receive string array names from my intent extras ( KI1,KI2, etc )

Instead of hardcoding the string array name (like R.array.KI1), I want variables replacing them .

strdata = receiverIntent.getExtras().getString("intentExtra");
String[] pointDetails =   getResources().getStringArray**(R.array.KI1);**

Resource file

<resources>
    <string-array name="KI1">
        <item>Categories Text KI1</item>
        <item>Unitary Channel Text KI1</item>
        <item>Localization Text KI1</item>
        <item>Action Text KI1</item>
        <item>Indication Text KI1</item>
        <item>Point location Text KI1</item>
        <item>KI1.png</item>
    </string-array>

    <string-array name="KI2">
        <item>Categories Text KI2</item>
        <item>Unitary Channel Text KI2</item>
        <item>Localization Text KI2</item>
        <item>Action Text KI2</item>
        <item>Indication Text KI2</item>
        <item>Point location Text KI2</item>
        <item>KI2.png</item>
    </string-array>
</resources>

Why is it not possible to do something like this :

   String[] pointDetails =   getResources().getStringArray("R.array." +   strdata);

I want the output of a variable to be like : R.array.strdata

woeiwkq
  • 1
  • 4

1 Answers1

0

because the getStringArray(); accepts int value but you passing string to it.

why dont you use switch case or if exp ... like

 String[] pointDetails ;

  switch(strdata)
  {
   case "KI1":
      pointDetails  = getResources().getStringArray(R.array.KI1);
   break;
   case "KI2":
      pointDetails  = getResources().getStringArray(R.array.KI2);
   break;
 }
Nickan
  • 466
  • 5
  • 17
  • If there are KI400, I would have to write 400 cases., But if I can get to know how to replace with a variable ., this would be 2 line of code . – woeiwkq Oct 04 '16 at 19:30
  • To achieve what you want, you need to get the resource by name instead of id. This is how: `Resources res = getResources(); int id = res.getIdentifier(strdata, "array", getContext().getPackageName()); String[] pointDetails = res.getStringArray(id);` – Georgios Oct 04 '16 at 19:37
  • Thanks @Giorgos, your solution works :) – woeiwkq Oct 04 '16 at 20:04