简体   繁体   中英

Putting resource identifiers into xml Array

In the first case, how do I put the the R.string resource identifier number into an integer array.

In the second case, how do I instead put the number associated with the R.integer resource identifier into the array.

<integer-array
    name="integer_array_name">
    <item>@string/nav_heading</item>
    <item>@integer/viewtype_heading</item>

    ....

</integer-array>

Using android studio if that makes any difference.

It appears one answer is to load the xml as a typed-array if you want to load just resource identifiers, and to load it as an integer-array if you want to load just resolved ints. You don't have to change the type of array in your xml, you can just leave it as a generic "array" as I've done in the tests

<array
    name="testArray">
    <item>@string/nav_vault_heading</item>
    <item>@integer/viewtype_heading</item>
</array>

//Test 1

    int[] intArray= getActivity().getResources().getIntArray(R.array.testArray);
    Log.d("MyInts", "IntArray = " + Arrays.toString(intArray));

    06-26 16:31:02.826  12952-12952/... D/MyInts﹕ IntArray = [0, 32]

//Test2

Due Credit: http://www.anddev.org/xml_integer_array_resource_references_getintarray-t9268.html

    TypedArray typedArray=   getActivity().getResources().obtainTypedArray(R.array.testArray);

    for(int i=0; i<typedArray.length(); i++){
        int id = typedArray.getResourceId(i, 0);
        Log.d("MyInts", "MyInts: " + id);
    }


06-26 16:31:02.826  12952-12952/... D/MyInts﹕ MyInts: 2131165274
06-26 16:31:02.826  12952-12952/... D/MyInts﹕ MyInts: 2131427334

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM