Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve 2D array from xml string resource for Android?

Tags:

Suppose I have stored a 2 dimensional array in android resource as shown below. How can I get them in a java collection like Arraylist?

<resources>
<string-array name="countries_array">   
<item>
    <name>Bahrain</name>
    <code>12345</code>
</item>
<item>
    <name>Bangladesh</name>
    <code>54545</code>
  </item>
<item>
    <name>India</name>
    <code>54455</code>
</item>

</string-array>
</resources>

For example in case of 1 dimensional array we can do it using

getResources().getStringArray(R.array.countries_array);

When the countries_array is like

<resources>
<string-array name="countries_array">   
  <item>Bahrain</item>
  <item>Bangladesh</item>
  <item>India</item>
</string-array>
</resources>
like image 773
Imon Avatar asked Jul 07 '12 17:07

Imon


2 Answers

The <string-array> element of a resources file can only be used for single dimension arrays. In other words, everything between <item> and </item> is considered to be a single string.

If you want to store data in the way you describe (effectively pseudo-XML), you'll need to get the items as a single String[] using getStringArray(...) and parse the <name> and <codes> elements yourself.

Personally I'd possibly go with a de-limited format such as...

<item>Bahrain,12345</item>

...then just use split(...).

Alternatively, define each <item> as a JSONObject such as...

<item>{"name":"Bahrain","code":"12345"}</item>
like image 76
Squonk Avatar answered Sep 19 '22 01:09

Squonk


Instead of multi-valued entries, I wrote about another approach where you can store your complex objects as an array, then suffix the name with an incremental integer. Loop through them and create a list of strongly-typed objects from there if needed.

<resources>
    <array name="categories_0">
        <item>1</item>
        <item>Food</item>
    </array>
    <array name="categories_1">
        <item>2</item>
        <item>Health</item>
    </array>
    <array name="categories_2">
        <item>3</item>
        <item>Garden</item>
    </array>
<resources>

Then you can create a static method to retrieve them:

public class ResourceHelper {

    public static List<TypedArray> getMultiTypedArray(Context context, String key) {
        List<TypedArray> array = new ArrayList<>();

        try {
            Class<R.array> res = R.array.class;
            Field field;
            int counter = 0;

            do {
                field = res.getField(key + "_" + counter);
                array.add(context.getResources().obtainTypedArray(field.getInt(null)));
                counter++;
            } while (field != null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return array;
        }
    }
}

It can be consumed like this now:

for (TypedArray item : ResourceHelper.getMultiTypedArray(this, "categories")) {
    Category category = new Category();
    category.ID = item.getInt(0, 0);
    category.title = item.getString(1);
    mCategories.add(category);
}
like image 22
Basem Avatar answered Sep 18 '22 01:09

Basem