0

i want to keep 100 question and its answer in String .xml , each question having multiple answer , some question having 5 answer and some question having 7 answerlike this .... I want to retrieve those question and answer in my java class

<string-array name="array01">
        <item name="question">question</item>
        <item name="answer1">answer1</item>
        <item name="answer2">answer2</item>
        <item name="answer3">answer3</item>
        <item name="answer4">answer4</item>
        <item name="answer5">answer5</item>
    </string-array>
    <string-array name="array02">

            <item name="question">question</item>
            <item name="answer1">answer1</item>
            <item name="answer2">answer2</item>
            <item name="answer3">answer3</item>
            <item name="answer4">answer4</item>
            <item name="answer5">answer5</item>
            <item name="answer6">answer6</item>

    </string-array>

    <string-array name="array03">
        <item name="question">question</item>
        <item name="answer1">answer1</item>
        <item name="answer2">answer2</item>
        <item name="answer3">answer3</item>
    </string-array>

    <array name="array0">
        <item>@array/array01</item>
        <item>@array/array02</item>
        <item>@array/array03</item>
    </array>

in java:

Resources res = getResources();
        TypedArray ta = res.obtainTypedArray(R.array.array0);
        int alength = ta.length();
        System.out.println(alength);//3
        String[][] array = new String[alength][];

        for (int i = 0; i < alength; ++i) {
            int id = ta.getResourceId(i, 0);

            if (id > 0) {
                array[i] = res.getStringArray(id);
                System.out.println(array[i]);//[Ljava.lang.String;@42637ec0
            } else {
                System.out.println("something wrong with the XML");
                // something wrong with the XML
            }
        }
        ta.recycle(); // Important!

    }
Trupti
  • 284
  • 5
  • 7
  • Where's your code? Did you try using the code in [this answer](http://stackoverflow.com/a/5931094/535871)? – Ted Hopp Apr 20 '14 at 17:51
  • this is my String .xml, yes i tried but i cant understand how to do ? – Trupti Apr 20 '14 at 17:53
  • If you tried that code, then the result should have been a two-dimensional array of `String` where each element is an array with the question as the first element and all its associated answers as the remaining elements. (The item names are pretty much irrelevant.) – Ted Hopp Apr 20 '14 at 17:56
  • just for understanding i have given the name, please give some code to make it correct – Trupti Apr 20 '14 at 17:58
  • You need to explain why this code doesn't work for you. – Ted Hopp Apr 20 '14 at 18:02
  • Actually i cant understand how to get each element of an array ? – Trupti Apr 20 '14 at 18:03
  • To get to each element, you just need to index twice; the first index will get to the array for a specific question; the second index will get the question (if the index is 0) or a specific answer (starting at index 1). If you are trying to print each array, use `Arrays.toString(array[i]);`. – Ted Hopp Apr 20 '14 at 18:05

0 Answers0