22

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>

2 Answers 2

42

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. But there is one important thing if there is a space in json its important to replace ' ' with special char i.e. {"title":"WHITE&#160;SHOW"}. Otherwise nothing will be shown. its more a feature of JSON
6

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);
}

2 Comments

The for loop throws an error at the getString line. Expected resource of type styleable.
@Nilpo Adding @SuppressWarnings("ResourceType") to the encapsulating method or class solves this. (This may only occur in newer versions of Android Studio.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.