简体   繁体   中英

How to get items from string array in java?

// strings.xml

<string-array name="list">
    <item>Istinye Park</item>
    <item>Kanyon Istanbul</item>
    <item>Akmerkez</item>
    <item>Forum Istanbul</item>
    <item>Istanbul Cevahir</item>
    <item>Metrocity</item>
    <item>Istanbul Sapphire</item>
</string-array>

I need to access specific item of string-array from java. I can access to string with R.string.str and to array with R.array.list . But need something like R.array.list[1] to access specific item of array.

You can load it via the resources with getStringArray , then just manipulate it like a standard array.

Resources res = getResources(); //assuming in an activity for example, otherwise you can provide a context.
String[] shoppingItems = res.getStringArray(R.array.shopping);
String istanbul = shoppingItems[1]; //Kanyon Istanbul

for the whole string array:

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

for single item from the string-array resource

    String myString = getResources().getStringArray(R.array.names)[numberOfItem];
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

您可以使用yourcontext.getResources().getStringArray(YOUR_ARRAY_ID)

使用getResources().getStringArray(R.array.shopping);

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