Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Resource - Array of Arrays

Tags:

java

android

xml

I am trying to implement a resource data structure that includes an array of arrays, specifically strings. The issue I run into is how to get the sub-array objects and their specific values. Here is what my resource file looks like....

<resources>    <array name="array0">      <item>        <string-array name="array01">          <item name="id">1</item>          <item name="title">item one</item>        </string-array>      </item>      <item>        <string-array name="array02">          <item name="id">2</item>          <item name="title">item two</item>        </string-array>      </item>      <item>        <string-array name="array03">          <item name="id">3</item>          <item name="title">item three</item>        </string-array>      </item>    </array> </resources> 

Then, in my Java code I retrieve the array and try to access the sub elements like so...

TypedArray typedArray = getResources().obtainTypedArray(R.array.array0);  TypedValue typedValue = null;  typedArray.getValue(0, typedValue); 

At this point the typedArray object should represent the string-array "array01", however, I don't see how to retrieve the "id" and "title" string elements. Any help would be appreciated, thanks in advance.

like image 883
JediPotPie Avatar asked Dec 01 '10 15:12

JediPotPie


2 Answers

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! 
like image 80
Ted Hopp Avatar answered Sep 16 '22 18:09

Ted Hopp


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); 
like image 40
Bill Avatar answered Sep 20 '22 18:09

Bill