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
Nandroid
  • 1
  • 1
  • 1

5 Answers5

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());
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
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.

Fabian
  • 356
  • 1
  • 5
  • 15
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

Community
  • 1
  • 1
Android_coder
  • 9,953
  • 3
  • 17
  • 23
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>
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
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)];
Michael A.
  • 4,163
  • 3
  • 28
  • 47