3

We can read a string array from xml file in android like mentioned here. But this can only read simple string array.

My question is that can we read array containing some other type of objects. xml file is like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array
        name="difficulty_level_names">
        <item>
            <name>IELTS</name>
            <type>1</type>
        </item>

        <item>
            <name>SAT</name>
            <type>2</type>
        </item>

        ................
        .........

    </string-array>
</resources>

UPDATE: Of course, there can be more parameters in each item..

Jin35
  • 8,602
  • 3
  • 32
  • 52

1 Answers1

2

No, in the way you provided. I have two solutions on my mind, and both not ideal.

1.

You can create multiple arrays, for example string-array for names, integer-array for integer etc. Every object would be stored in those arrays at the same index. It's not simple to maintain but it may be more memory efficient in case of accesing only one property from object.
I described it in: https://stackoverflow.com/a/6774856/538169

Or

2.

You can create one typed array and have object's properties in the array. You could access the property through translating the index in such way:

final int PROPERTIES_COUNT_IN_OBJECT = 2;
int objectIndex   = 0; // the first element in your's example
int propertyIndex = 1; // the second property: <type>1</type>
int indexInArray  = objectIndex * PROPERTIES_COUNT_IN_OBJECT + propertyIndex;

Then get this property from TypedArray.

The second solution is much worse to maintain. Adding another property would be a nightmare.

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • +1 First solution looks elegant, I am yet to try it. Hang on please –  Feb 06 '12 at 15:21