Android Resource - Array of Arrays
Solution 1:
You can almost do what you want. You have to declare each array separately and then an array of references. Something like this:
<string-array name="array01">
<item name="id">1</item>
<item name="title">item one</item>
</string-array>
<!-- etc. -->
<array name="array0">
<item>@array/array01</item>
<!-- etc. -->
</array>
Then in your code you do something like this:
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.array0);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
} else {
// something wrong with the XML
}
}
ta.recycle(); // Important!
Solution 2:
https://developer.android.com/guide/topics/resources/more-resources.html#Color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>@drawable/home</item>
<item>@drawable/settings</item>
<item>@drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
This application code retrieves each array and then obtains the first entry in each array:
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);
Solution 3:
according to @Ted Hopp answer, more elegant way:
<integer-array name="id_array">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
<string-array name="name_array">
<item>name 1</item>
<item>name 2</item>
<item>name 3</item>
</string-array>
<array name="array0">
<item>@array/id_array</item>
<item>@array/name_array</item>
</array>
make sure sub arrays row count is identical.
enjoy write code to access array cells!
android is still a kid while maintain the ugly "item" tag of the TypedArray "array0".
in my opinion, the most flexible should be:
<array name="array0">
<integer-array name="id_array">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
<string-array name="name_array">
<item>name 1</item>
<item>name 2</item>
<item>name 3</item>
</string-array>
</array>
but don't do that because that's not android way :)