从android中的string.xml中检索二维数组

时间:2014-04-20 17:47:54

标签: android arrays android-resources

我想在String .xml中保留100个问题及其答案,每个问题都有多个答案,一些问题有5个答案,有些问题有7个答案就像这样....我想在我的java中检索那些问题和答案类

<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>

在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!

    }

0 个答案:

没有答案