Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Arrays in Android xml

Previously I had a simple string-array that contained a URL it looked like

    <string-array name="drawerlinkitems">
      <item>http://www.google.com</item>
      <item>http://www.google2.com</item>
      <item>http://www.google3.com</item>
    </string-array>

and i was able to access the values with the call

return getResources().getStringArray(R.array.drawerlinkitems)[number];

pretty simple stuff.

my problem at this point is I want to do some more actions other than just grabbing the url, so i would like to build a nested array, like this:

<string-array name="draweritems">
    <item>
        <link>http://www.google.com</link>
        <title>Google</title>
        <icon>soon</icon>
    </item>
    <item>
        <link>http://www.google2.com</link>
        <title>Google2</title>
        <icon>soon</icon>
    </item>
    <item>
        <link>http://www.google3.com</link>
        <title>Google3</title>
        <icon>soon</icon>
    </item>
</string-array>

and then access it, using something like

getResources().getStringArray(R.array.draweritems)[number].getString[link];

or

getResources().getStringArray(R.array.draweritems)[number].getString[1];

(obviously i made the getString part up)

I can't figure out if it's even possible to do this in string-arrays, and if not what the replacement option is. If it is, i'm not sure exactly how to reference the string array to get the child values out of the item parent. i'm also not tied down to this type of solution if there's a superior way to do this that you know of. any help would be much appreciated.

like image 469
Josh Avatar asked Sep 09 '14 18:09

Josh


People also ask

What is an array in Android?

What is an Array? An array is a common data structure used to store an ordered list of items. The array elements are typed. For example, you could create an array of characters to represent the vowels in the alphabet: 1.


1 Answers

This is what I've done to accomplish something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="menu_items">
        <item>@array/menu_item_dashboard</item>
        <item>@array/menu_item_index</item>
    </array>

    <array name="menu_item_dashboard">
        <item>@drawable/transparent</item>
        <item>Dashboard</item>
        <item>home</item>
    </array>
    <array name="menu_item_index">
        <item>@drawable/transparent</item>
        <item>Title</item>
        <item>index</item>
    </array>
</resources>

And to access:

TypedArray menuResources = getResources().obtainTypedArray(R.array.menu_items);

TypedArray itemDef;

for (int i = 0; i < menuResources.length(); i++) {
    int resId = menuResources.getResourceId(i, -1);
    if (resId < 0) {
        continue;
    }

    itemDef = getResources().obtainTypedArray(resId);
    //itemDef.getDrawable(0)
    //itemDef.getString(1)
    //itemDef.getString(2)
}
like image 184
mstrthealias Avatar answered Sep 22 '22 23:09

mstrthealias