0

I have an array of Images, and I am trying to change image views to match these drawables. For some reason, they are simply not being drawn at all.

Here is my code so far:

TypedArray answerResources = res.obtainTypedArray(R.array.answers);
int resId = answerResources.getResourceId(randomQuestion, -1);
answerResources.recycle();

int [] questionAnswers = res.getIntArray(resId);

firstAnswerImg .setImageResource(questionAnswers[0]);
secondAnswerImg.setImageResource(questionAnswers[1]);
thirdAnswerImg .setImageResource(questionAnswers[2]);
fourthAnswerImg.setImageResource(questionAnswers[3]);

Here is the xml:

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

    <integer-array name = "questionOneAnswers">
        <item>@drawable/da</item>
        <item>@drawable/sk</item>
        <item>@drawable/cq</item>
        <item>@drawable/ht</item>
    </integer-array>

What am I missing??

1 Answer 1

1

EDIT: My bad, I see the nesting now. I think you need to use typed arrays (just like you did for the outer ones), like so:

<array name="answers">
  <item>@array/questionOneAnswers</item>
  <item>@array/questionTwoAnswers</item>
  <item>@array/questionThreeAnswers</item>
</array>
...
<array name="questionOneAnswers">
  <item>@drawable/da</item>
  <item>@drawable/sk</item>
  <item>@drawable/cq</item>
  <item>@drawable/ht</item>
</array>

Then, pull from the array like so:

TypedArray answers = res.obtainTypedArray(R.array.answers);
int questionOneAnswersId = answerResources.getResourceId(0, 0));
TypedArray questionOneAnswers = res.obtainTypedArray(questionOneAnswersId);
firstAnswerImg.setImageDrawable(questionOneAnswers.getDrawable(0);
....
Sign up to request clarification or add additional context in comments.

2 Comments

this works, but not what I'm looking for. I want to access it through nested arrays. See my original xml
Ah sorry, missed that. Edited my answer, check it out now.

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.