3
// 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.

4
  • You have there code. Which code? Commented Feb 11, 2015 at 12:11
  • 2
    String[] shopping = getResources().getStringArray(R.array.shopping); For your reference: developer.android.com/guide/topics/resources/… Commented Feb 11, 2015 at 12:12
  • I have there just string-array and I know how to access to array but I don't know how to access to some specific item (from java). For ex: I need to access to "Istinye Park" with single code line, How I can get it? Commented Feb 11, 2015 at 12:15
  • String istinyePark = shopping[0]; Commented Feb 11, 2015 at 12:17

5 Answers 5

14

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
Sign up to request clarification or add additional context in comments.

Comments

4

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];

Comments

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

Comments

0

You can use yourcontext.getResources().getStringArray(YOUR_ARRAY_ID)

Comments

0

Use getResources().getStringArray(R.array.shopping);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.