5

I have an integer array in an xml file as follows

<integer-array name="myArray">
    <item>@drawable/pic1</item>
    <item>@drawable/pic2</item>
    <item>@drawable/pic3</item>
    <item>@drawable/pic4</item>
</integer-array>

In the code, I am trying to load this array

int[] picArray = getResources().getIntArray(R.array.myArray);

The expected result is

R.drawable.pic1, R.drawable.pic2,R.drawable.pic3

but instead it is coming with an array with all values as zero

Can anyone tell me what is wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
GSree
  • 2,890
  • 1
  • 23
  • 25
  • 2
    have you tried to put just integers?! – Tima Dec 14 '10 at 22:35
  • My Xml as follows. The java code getResources().getIntArray(R.array.myArray) gives incorrect result whereas getResources().getStringArray(R.array.myWord) is giving correct result Word1 word2 @drawable/pic1 @drawable/pic2 @drawable/pic3 – GSree Dec 14 '10 at 23:00

4 Answers4

12

Found this solution:

TypedArray ar = context.getResources().obtainTypedArray(R.array.myArray);
int len = ar.length();

int[] picArray = new int[len];

for (int i = 0; i < len; i++)
    picArray[i] = ar.getResourceId(i, 0);

ar.recycle();

// Do stuff with resolved reference array, resIds[]...
for (int i = 0; i < len; i++)
    Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(picArray[i]));

And resources xml file could be:

<resources>
    <integer-array name="myArray">
        <item>@drawable/pic1</item>
        <item>@drawable/pic2</item>
        <item>@drawable/pic3</item>
        <item>@drawable/pic4</item>
    </integer-array>
</resources>
Mixaz
  • 4,068
  • 1
  • 29
  • 55
3

It looks like you might be talking about typed arrays?

if so a typed array should look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

Can you show us your actual xml file so we can help you?

EDIT: Yeah those are not integers. make it a resource array if you want to store resources.

Pure Function
  • 2,129
  • 1
  • 22
  • 31
  • My Xml as follows. The java code getResources().getIntArray(R.array.myArray) gives incorrect result whereas getResources().getStringArray(R.array.myWord) is giving correct result Word1 word2 @drawable/pic1 @drawable/pic2 @drawable/pic3 – GSree Dec 14 '10 at 23:01
  • I'm still unclear as to what you're doing, your second example will give correct results because you're getting a string array which is what it expects, but your integer array in xml doesn't have integers in it it has resource paths. I think you should check out Mur Votema's answer. seems to be what you might be trying to do. – Pure Function Dec 14 '10 at 23:06
  • I have the list under resources.xml. Tried using array instead of Integer-array. How do I load this array object? I used getResources().getIntArray(R.array.newarrayname) and still getting same result (i.e. and array of zeros). I thought @drawable/home will be stored as int! – GSree Dec 14 '10 at 23:14
  • 1
    no its not stored as int. i can see how you would make that conclusion because in android code they are ints but only after being converted by java. when you make an int-array it has to contain ints, as in: 1, 2, 3. integer primitives. don't use getIntArray because they are not ints. you could use getstringarray if you want to store the paths in a string, but as I said I'm still unclear what you're doing and Mur Votema's link to the tutorial seems to be the type of thing you want to do. – Pure Function Dec 14 '10 at 23:25
  • Thanks. Now I understand. I think I should follow Mur Votema's code snippet. – GSree Dec 14 '10 at 23:34
2

You need to get an array with id's of your images. Probably this article helps you. And so the code you probably need:

int[] picArray = new int[4];

for (int i = 1; i <=4; i++)
{
  try 
  {
    Class res = R.drawable.class;
    Field field = res.getField("pic"+i);
    picArray[i-1] = field.getInt(null);
  }
  catch (Exception e)
  {
    Log.e("MyTag", "Failure to get drawable id.", e);
  }
}
Tima
  • 12,765
  • 23
  • 82
  • 125
  • I can see reflection classes are used here. Is this the only way? Is there a way I can make getResources.getIntArray work? – GSree Dec 14 '10 at 23:06
1

Just make it a normal resource array. You could do it like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <array name="icons">
       <item>@drawable/home</item>
       <item>@drawable/settings</item>
       <item>@drawable/logout</item>
   </array>
</resources>

Then don't make a int[] just make a TypedArray like this:

TypedArray icons = getResources().obtainTypedArray(R.array.icons);

and get it with:

imageview.setImageDrawable(mIcons.getDrawable(position));
Johannes
  • 21
  • 4