3

I'm looking for option to access array of strings defined in strings.xml in loop. Means, I have in my strings.xml - several arrays like:

<string-array name="ques1">
    <item>bla bla</item> 
    <item>bla bla</item>
</string-array>
<string-array name="ques2">
    <item>bla bla</item> 
    <item>bla bla</item>
</string-array>

How can i access in my code - the arrays of ques1, ques2...etc ? (for example - i have many questions in my application with its answers, so i want to access it in generic way).

1 Answer 1

6
String[] stringArray = context.getResources().getStringArray(R.array.ques1);
for(String s : stringArray) {
     //do something with s
}

Edit ... I just realized you want to iterate not over the array, but over several arrays. That's also possible, with :

for(int i = 0; i<max_arrays; i++) {

    int id = context.getResources().getIdentifier("ques"+i, "array", 
    context.getPackageName());

    String[] stringArray = context.getResources().getStringArray(id);

    for(String s : stringArray) {
         //do something with s
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! this is exactly what i needed - Iteration over the arrays themselves. thank you very much.

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.