0

I tried to declare array with items which has property type="id"

<item name="viewpager_id_base" type="id"/>
<integer-array name="viewpager_dates" type="id">
    <item type="id">0</item>
    <item type="id">1</item>
    <item type="id">2</item>
    <item type="id">3</item>
    <item type="id">4</item>
    <item type="id">5</item>
    <item type="id">6</item>
    <item type="id">7</item>
    <item type="id">8</item>
    <item type="id">9</item>
    <item type="id">10</item>

</integer-array>

But id does not created ids in R file. Only id for viewpager_id_base appiared

Yarh
  • 4,459
  • 5
  • 45
  • 95

3 Answers3

8

I know this question was a long time ago but someone might find my answer useful.

Sample xml:

<resources>
    <item name="first_id" type="id" />
    <item name="second_id" type="id" />
    <item name="third_id" type="id" />

    <array name="default_ids">
        <item>@id/first_id</item>
        <item>@id/second_id</item>
        <item>@id/third_id</item>
    </array>
</resources>

Getting the array of ids:

TypedArray typedArray = getResources().obtainTypedArray(R.array.default_menu_ids);
int[] ids = new int[typedArray.length()];
for(int ind=0; ind<typedArray.length(); ind++){
    ids[ind] = typedArray.getResourceId(ind,0);
}

typedArray.recycle();

The second argument of getResourceId is default id in case you messed something.

(Honestly I don't know why people downvoted this question. The question is very clear.)

hehe
  • 1,294
  • 13
  • 26
1

Use below code :

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

see below link for more information :-

Referencing a string in a string array resource with xml

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • I like that approach and it works with string. But I cannot reference `item` like @item/day1. Do you know a possible workaround? – Yarh Oct 28 '14 at 11:04
1

seems you can't do that in xml file. try this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="id1" type="id"/>
    <item name="id2" type="id"/>
    <item name="id3" type="id"/>
    <item name="id4" type="id"/>
    <item name="id5" type="id"/>
</resources>

and in your java code:

int[] idArrays = new int[]{R.id.id1, R.id.id2, R.id.id3, R.id.id4, R.id.id5} ;
WoookLiu
  • 402
  • 2
  • 14