Retrieve 2Dimensional array from string.xml in android

浪子不回头ぞ 提交于 2020-01-06 10:51:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!