1

I would like to retrieve the layouts' address using a from my java file. However, what I get is 0.

res/values/worlds.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array
        name="world1">
        <item>@layout/lvl_1</item>
        <item>@layout/lvl_1</item>
    </integer-array>
</resources>

and the Java code which I use to get the array:

Resources res = getResources();
int[] layouts = res.getIntArray(R.array.world1);
setContentView(layouts[0]);

the Java code gives me a 0 on my Logcat though.

  • possible duplicate of [Loading Interger Array from xml](http://stackoverflow.com/questions/4444950/loading-interger-array-from-xml) – Merlevede Feb 28 '14 at 01:18

1 Answers1

4
<item>@layout/lvl_1</item>  

is not type of Integer. so use array in xml to store items and obtainTypedArray in code for getting Array of items from xml then use getResourceId to pass layout id to setContentView as :

res/values/worlds.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array
        name="world1">
        <item>@layout/lvl_1</item>
        <item>@layout/lvl_1</item>
    </array>
</resources>

In code set layout as:

TypedArray layouts = getResources().obtainTypedArray(R.array.world1);
int layoutid=layouts.getResourceId(0, -1);
setContentView(layoutid);
layouts.recycle();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • This worked perfectly! Forgive me but, if I may add, is there any way to store this typed array to a ? I would want to use Collections.shuffle on the retrieved layouts' ids – Kenn de Leon Feb 28 '14 at 03:18