0

I am creating a simple quiz app, that could use the resources of the array.xml (no external parser, no adapters)

<string-array name="Question1">
        <item name="Question">This is Question1</item>
        <item name="Answer">option1</item>
        <item name="option1">option2</item>
        <item name="option2">option3</item>
        <item name="option3">option4</item>
</string-array>

I got all the elements with this code

String[] quesArray = getResources().getStringArray(R.array.Question1);

Now I want to get the string “Answer” , so that it can be further used and manipulated How Is it possible? I tried it with

int resId = context.getResources().getIdentifier("Answer", "string", getPackageName());

but it gives error

android.content.res.Resources$NotFoundException: String resource ID #0x0
0

5 Answers 5

1

Try out as below:

<string-array name="Question1">
    <item>This is Question1</item>
    <item>option1</item>
    <item>option2</item>
    <item>option3</item>
    <item>option4</item>
 </string-array>
 String[] quesArray = getResources().getStringArray(R.array.Question1);
 int resId = context.getResources().getIdentifier("Answer", "string", getPackageName());
Sign up to request clarification or add additional context in comments.

Comments

0

A string array is propperly formatted like this:

<string-array name="question1">
<item>option 1</item>
<item>option 2</item>
</string-array>

I've never seen it the way you use it.
http://developer.android.com/guide/topics/resources/string-resource.html for more information.

Comments

0

Try this:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
<string-array name="testArray">  
    <item>first</item>  
    <item>second</item>  
    <item>third</item>  
    <item>fourth</item>  
    <item>fifth</item>  
 </string-array>
</resources>

And also try this link this will be help to you:

Help in getting String Array from arrays.xml file

Comments

0
<string name="question">This is Question1</string>
<string name="answer">Answer</string>

<string-array name="question1">
    <item>@string/question</item>
    <item>@string/answer</item>
</string-array>

Comments

0

A StringArray is not a String resource (which is what you are asking for in the getIdentifier).

Given that you have a fixed structure in your string array with Question, Answer, Options, the simplest way to do what you are suggestion would be to simply just use the fixed index that you have adopted. That is - given an array as such:

<string-array name="Question1">
    <item>This is Question1</item>
    <item>1</item>
    <item>option1</item>
    <item>option2</item>
    <item>option3</item>
</string-array>

Access your answer as follows (note that I replaced your answer "string" with an index to the right answer):

final int QUESTION = 0;
final int ANSWER = 1;
String question = quesArray[QUESTION];
String answer = quesArray[Integer.parseInt(quesArray[ANSWER]) + 1)];

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.