2

let's say i have this Multidimensional Array :

myArray = new String[][]{{"Hello","World"},{"I Love","Android"},{"something","Random"}};

and in my app , i'm dealing with it like this :

for (int i=0; i<myArray.length; i++) {
    for (int x=0; x<myArray[i].length; x++) {
    // set textview or something 
    }
}

I want to do this Multidimensional Array by XML resource file.

i tried this way but did not work for me :

<string-array name="myArray">
    <item><item>Hello</item><item>World</item></item>
    <item><item>I Love</item><item>Android</item></item>
    <item><item>something</item><item>Random</item></item>
</string-array>

I know I can do normal string-array in the XML file and make it look like Multidimensional Array by Java but I just want to know if it's possible to do it in XML directly

2 Answers 2

3

You can maintain your xml for array like this:

<resources>
<array name="categories_0">
    <item>1</item>
    <item>Pizza</item>
</array>
<array name="categories_1">
    <item>2</item>
    <item>Burger</item>
</array>
<array name="categories_2">
    <item>3</item>
    <item>Maggie</item>
</array>

Now each category is an array with a key/value pair for it’s properties. What ties it with other categories is the integer suffix. Now we can use this dandy static method to grab them: Than, we can define global reference for index of the array:

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

This is dynamically retrieving the resources programmatically, with an incremented counter to find the next object until there isn’t one left. Now this can be consumed throughout your code base like this:

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

Here you may face error in encapsulating class or method. You can just add @SuppressWarnings("ResourceType") to that method or class.

This example will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

it would be amazing if you give me a full example code, i'm still a beginner
1

It's impossible. You can put Json String inside <item>{"dummy":"dummy"} </item> and then parse it.

1 Comment

still have no clue how to do that ... but thanks anyway

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.