Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference string array res id from an array

Tags:

android

xml

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.

like image 781
Snake Avatar asked Jul 25 '26 14:07

Snake


2 Answers

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>
like image 187
Rohit Suthar Avatar answered Jul 27 '26 04:07

Rohit Suthar


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>
like image 29
nem035 Avatar answered Jul 27 '26 02:07

nem035