XML integer array, resource references, getIntArray

Quickly share your Android Code Snippets here...

XML integer array, resource references, getIntArray

Postby XCaffeinated » Mon Nov 30, 2009 1:11 am

If you're reading this then you probably already know that given:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <integer-array name="my_array">
  3.     <item>@drawable/my_pic1</item>
  5.     <item>@drawable/my_pic2</item>
  7. </integer-array>
Parsed in 0.001 seconds, using GeSHi

The following WILL NOT WORK:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. getResources().getIntArray(R.array.my_array);
Parsed in 0.011 seconds, using GeSHi

The returned references will be 0.

Here is a workaround for this issue, using the xml shown above:

This concept was adapted from the Android source code at: http://www.netmite.com/android/mydroid/ ... eInit.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array);
  5. int len = ar.length();
  7. int[] resIds = new int[len];
  9. for (int i = 0; i < len; i++)
  11.     resIds[i] = ar.getResourceId(i, 0);
  13. ar.recycle();
  17. // Do stuff with resolved reference array, resIds[]...
  19. for (int i = 0; i < len; i++)
  21.     Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(resIds[i]));
Parsed in 0.010 seconds, using GeSHi

TypedArray ar has an .mData member containing among other things, a resolved resource id, which can be retrieved with .getResourceId(), though this is not immediately obvious. I.e., each index of ar is being treated as an attribute, and .getResourceId() gets the resource id for that attribute.

Tested on Android versions 1.1 and 2.0, emulator only.

Additional Notes
- This is a workaround for .getIntArray() failing to resolve resource references (e.g., '@drawable/my_pic1') from integer-array resources. Once .getIntArray() is fixed there will be no need for the above snippet.

- .obtainTypedArray() is retrieving from a compiled resource, so this mechanism should be fast.

- Don't forget to call .recycle().

Interesting aside: Be aware that if you call .recycle() then declare a new TypedArray ar1 = ... you will see the old value (the one that is no longer valid) change in the debugger variable window if it's still present. This threw me for a loop the first time I saw it, until I realized that .recycle() literally recycles that area of memory for use by the next instance of TypedArray, so when you use the new instance you are referencing the same memory location. Obviously the old one is no longer valid, but you may still see it update if it's present in the variable window.

- Hint of the Day: In your Eclipse IDE, turn your variables into hex output so you can easily check your resource ids against R.Java when you debug. In Eclipse (Galileo): Window->Preferences->Java->Debug->Primitive Display Options, check 'Display hexadecimal values'.

Hope this helps!
Posts: 25
Joined: Sun Nov 29, 2009 10:16 pm


Re: XML integer array, resource references, getIntArray

Postby diesalher » Tue Jun 22, 2010 10:54 am

Fantastic! Thanks a million.
Once Poster
Once Poster
Posts: 1
Joined: Tue Jun 22, 2010 10:15 am

Re: XML integer array, resource references, getIntArray

Postby Aseriesofsmallprojects » Wed Sep 28, 2011 1:04 am

Nice work! I created a utility function with this code, and now I can load multidimensional arrays (grouped sets of images) with a minimal amount of code.
www.ProjectJourneyman.com - Android development and app marketing
twitter.com/PrjJourneyman Twitter feed
Android Developer Tutorial A growing tutorial for Android Devs
The Android Income Series Books on income topics for Android developers
Posts: 39
Joined: Fri Oct 08, 2010 4:29 pm
Location: USA


Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests