0

Suppose I'm having a three-dimensional array in my arrays.xml, something like

<string-array name="level1-1">
    <item name="id">a</item>
    <item name="id">b</item>
</string-array>
<string-array name="level1-2">
    <item name="id">c</item>
    <item name="id">d</item>
</string-array>
<array name="level1">
    <item>@array/level1-1</item>
    <item>@array/level1-2</item>
</array>
<string-array name="level2-1">
    <item name="id">e</item>
    <item name="id">f</item>
</string-array>
<string-array name="level2-2">
    <item name="id">g</item>
    <item name="id">h</item>
</string-array>
<array name="level2">
    <item>@array/level2-1</item>
    <item>@array/level2-2</item>
</array>
<array name="top_level">
    <item>@array/level1</item>
    <item>@array/level2</item> 
</array>

Now I'm looking to read it inside of my Android application. From reading similar questions here I've made a conclusion that TypedArrays are the proper way of retrieving data and while it's not hard to retrieve the whole 3-dimensional chunk as a TypedArray, it doesn't seem like there are any instruments for retrieving another TypedArrays using their IDs.

So how it is possible to retrieve a 2-dimensional "slice" out of a 3-dimensional TypedArray?

2 Answers 2

1

The firs you can get your array from xml

   List<String> tablel1-1 = Array.asList(getResources().getStringArray(R.array.table1-1));
   List<String> tablel1-2 = Array.asList(getResources().getStringArray(R.array.table1-1));

   HashMap<List<String>,List<String>> level1 = new HashMap<List<String>,List<String>(table1-1,table1-2);


   List<String> tablel2-1 = Array.asList(getResources().getStringArray(R.array.table2-1));
   List<String> tablel2-2 = Array.asList(getResources().getStringArray(R.array.table2-1));

   HashMap<List<String>,List<String>> level2 = new HashMap<List<String>,List<String>(table2-1,table2-2);

Finally you can create the HashMap with two HashMaps if necessary.

    Hashmap<Hashmap<List<String>,List<String>>,Hashmap<List<String>,List<String>>> top_level = new
                 Hashmap<Hashmap<List<String>,List<String>>,Hashmap<List<String>,List<String>>>(level1,level2);
Sign up to request clarification or add additional context in comments.

4 Comments

While this works with the said example I'm not sure this will work with bigger arrays or more dimensions, I just tried to give the easiest example. Let's generalize: we have a TypedArray array with N dimensions and each dimension has M(i) elements where i belongs to [1..n]. We are given an index K. How do we retrieve array[K] using arrays.xml only to retrieve the array itself?
ArrayList in Java has a get(int index) method. int is a signed 32 bit value, with a maximum value of 2,147,483,647.
A HashMap uses an array which always has a size that is a power of two, so it can be at most 2^30 = 1073741824 elements
Right, but what would you do if there were, say, 5 dimensions with 10 elements in each? I'm looking for a more universal approach.
0

Seems like I've already figured it out by myself. Some Java code to perform the task of retrieving the "slice" like in the example shown above:

public TypedArray getSlice(TypedArray ta, long k)    {
    Resources res = getResources();

    int id = ta.getResourceId((int)k, 0);
    return res.obtainTypedArray(id);
}

As with the example given in the question the function call should be getSlice(array, K).

Comments

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.