简体   繁体   中英

How to list Android string resources in an array

I want to do something similar to this as explained in Reference drawable from string-array in android

private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

I tried this .java file :

    int[] descriptions = new int[] { 
        R.string.description_section1, 
        R.string.description_section2, 

    }; 

but it doesn't seem to be the right syntax.

Try this as mentioned in the docs :

int[] descriptions = { 
    R.string.description_section1, 
    R.string.description_section2, 

}; 

define your array in array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="descriptions">
        <item>section1</item>
        <item>section1</item>
    </string-array>
 </resources>

then try this:

String[] descriptions = getResources().getStringArray(
                R.array.descriptions);

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