问题
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!
}
来源:https://stackoverflow.com/questions/23185724/retrieve-2dimensional-array-from-string-xml-in-android