3

I have the following XML

<string-array name="str_arr1">
  <item>My item 1</item>
  <item>My item 2</item>
  <item>My item 3</item>
</string-array>

<string-array name="str_arr2">
  <item>My item 4</item>
  <item>My item 5</item>
  <item>My item 6</item>
</string-array>

How can I reference the above strings arrays using an array.. something like below (maybe another type of array needs to be used?)

<string-array name="my_strings_arrays">
  <item>R.array.str_arr1</item>
  <item>R.array.str_arr2</item>
</string-array>

Basically in the code, I want to read the my_strings_arrays and then for each array , I want to grab the list of items within.

nem035
  • 34,790
  • 6
  • 87
  • 99
Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

3

In any xml resource file we can use reference of array/string/integer/color/etc. with "@" prefix. So, here we can use @array for getting reference of another array str_arr1 and str_arr2.

<string-array name="str_arr1">
  <item>My item 1</item>
  <item>My item 2</item>
  <item>My item 3</item>
</string-array>

<string-array name="str_arr2">
  <item>My item 4</item>
  <item>My item 5</item>
  <item>My item 6</item>
</string-array>

You to need change your code as below.

<string-array name="my_strings_arrays">
  <item>@array/str_arr1</item>
  <item>@array/str_arr2</item>
</string-array>
Rohit Suthar
  • 2,635
  • 1
  • 22
  • 27
2

Your second array should be an array of string arrays, not string array of string arrays.

I think the way you would do what you need is:

<array name="my_strings_arrays">
    <item>@array/str_arr1 </item>
    <item>@array/str_arr2 </item>
</array>
nem035
  • 34,790
  • 6
  • 87
  • 99
  • hmm interesting, can you update the answer with how you can load such thing using code? For example, how to display item1? Thank you so much – Snake Nov 29 '15 at 04:28
  • Actually, [here's a good example already](http://stackoverflow.com/a/5931094/3928341) instead of me typing it here. – nem035 Nov 29 '15 at 04:29
  • @Snake no problem, glad to help :) – nem035 Nov 29 '15 at 04:41