I'm attempting to set up arrays of resource ids in XML and choosing
from them during runtime, but it appears the references are not
resolving as I would expect. Here is an example of what I am doing in
the arrays.xml file in my res/values directory:
<resources>
<string-array name="pets">
<item>cat</item>
<item>dog</item>
</string-array>
<integer-array name="cat">
<item>@drawable/cat</item>
<item>@raw/cat_meow1</item>
<item>@raw/cat_meow2</item>
</integer-array>
<integer-array name="dog">
<item>@drawable/dog</item>
<item>@raw/dog_bark1</item>
<item>@raw/dog_bark2</item>
</integer-array>
</resources>
And in my code:
private void setPet(String petType) {
int arrayId = mResources.getIdentifier(petType, "array",
getPackageName());
int[] petResids = mResources.getIntArray(arrayId);
BACKGROUND = petResids[0];
SOUND_PET1 = petResids[1];
SOUND_PET2 = petResids[2];
}
What I am seeing:
- The arrayId value is getting set to the correct resource id.
- If I hardcode the resource ids (from R.java) in array.xml,
everything works.
- However, using the "@" notation does not work -- the petResids
array is filled with zeroes.
I know that one can reference strings from the strings.xml file,
colors from the colors.xml file, etc. Is it the case that the @
references are evaluated for only some XML files (like those in the
layout or menu directories)? Or am I missing something more
fundamental?
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en