4

Suppose I have a resource xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <array name="difficulties">
        <item>
            <integer name="level">1</integer>
            <integer name="fixed_blocks">2</integer>
            <integer name="color_count">2</integer>
        </item>
        <item>
            <integer name="level">2</integer>
            <integer name="fixed_blocks">4</integer>
            <integer name="color_count">3</integer>
        </item>
        <item>
            <integer name="level">3</integer>
            <integer name="fixed_blocks">6</integer>
            <integer name="color_count">3</integer>
        </item>
    </array>    
</resources>

How can I get the integer values from an item by name? The TypedValue's API doesn't seem to contain any methods for this. If this is not possible with the TypedArray how IS it?

If I can get a value from an item by its ordinal it will be OK too.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207

1 Answers1

6

I don't recall this being possible(but I could be wrong). Judging by the structure of an item(in the difficulties array) you could do something else, you could use an array of integer arrays. Knowing that the item array has level at the first position, fixed_blocks as the second position etc you could easily get the values. An example of this you can find here Android Resource - Array of Arrays

Edit: Is this the method you're looking for?

private int[] getLevelConstants(int level) {
    int[] result = null;
    TypedArray ta = getResources().obtainTypedArray(R.array.difficulties);
    // keep in mind the differences between the level(starts at 1) and the index of the difficulties array which starts at 0
    int id = ta.getResourceId(level, 0); 
    if (id > 0) {
        result = getResources().getIntArray(id);
    } else {
        // something bad
    }
    return result;
}

and the array will be:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <integer-array name="difficulties">
        <item>@array/level1</item>
        <item>@array/level2</item>
    </integer-array>

    <integer-array name="level1" >
        <item>1</item>
        <item>2</item>
        <item>2</item>
    </integer-array>

    <integer-array name="level2" >
        <item>2</item>
        <item>4</item>
        <item>3</item>
    </integer-array>

</resources>
Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • The problem with this is that I receive an index from another activity which points to a specific difficulty (so I get 2 for example) and I need to get the element from the TypedArray by index and after that check out its elements. – Adam Arold Oct 01 '12 at 15:08
  • @AdamArold By difficulty 2 you mean `level` 2 in the array item, right? If this is the case then the `level` integer has a simple mapping with the index of the `difficulties` array, you just have to subtract `1`(the `difficulties` array starts the index at 0 and the level starts from 1) from the index and there you have it. – user Oct 01 '12 at 15:17
  • No the problem is that I can't use R.array.difficulties.level1 for example since I can't hardcode that in my program, I only have an index. I can't use R.array.difficulties either since I can't extract my iteger-array from it with a method, I can only extract string-arrays. So I don't have the Resource id I need in order to look it up from Resources I just have an ordinal for an internal element of a resource. – Adam Arold Oct 01 '12 at 15:20
  • Sorry I wasn't aware of getResourceId() – Adam Arold Oct 01 '12 at 16:10