Sadik Sadik - 2 months ago 17
Android Question

How to get items from string array in java?

(strings.xml)

<string-array name="shopping">
<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 have here code. And I need to access to specific item of string-array from java. For ex: to string: it is like- R.string.something, it is easy. And I can access to array too, like, R.array.shopping. But after it to some item I can't, I tried like, R.array.shopping[1] or something like that but it didn't work. Please can you help me, I need to know how to access to some specific item with single line code.

Answer Source

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