1

Hi I'm pretty new to android (and here) and I am trying to do this:

    Random random = new Random();
    int randomQuestion = random.nextInt(3);

    Resources res = getResources();
    String[] questions = res.getStringArray(R.array.question_array);
    String[] answers = res.getStringArray(R.array.answers[randomQuestion]);

But that last line clearly doesn't work. My xml file looks like this:

 <array name = "answers">
    <item>@array/questionOneAnswers</item>
    <item>@array/questionTwoAnswers</item>
    <item>@array/questionThreeAnswers</item>
</array>

    <string-array name = "questionOneAnswers">
        <item>Question 1, Answer 1</item>
        <item>Question 1, Answer 2</item>
        <item>Question 1, Answer 3</item>
        <item>Question 1, Answer 4</item>
    </string-array>
    <string-array name = "questionTwoAnswers">
        <item>Question 1, Answer 1</item>
        <item>Question 1, Answer 2</item>
        <item>Question 1, Answer 3</item>
        <item>Question 1, Answer 4</item>
    </string-array>
    <string-array name = "questionThreeAnswers">
        <item>Question 1, Answer 1</item>
        <item>Question 1, Answer 2</item>
        <item>Question 1, Answer 3</item>
        <item>Question 1, Answer 4</item>
    </string-array>

I also cant do this:

String[] [] answers = res.getStringArray(R.array.answers);

How do I go about accessing a nested array?

1
  • 1
    Take a look at this post Commented Jun 18, 2018 at 19:06

0

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.