从strings.xml获取数组

时间:2016-09-06 07:00:17

标签: android xml android-resources

我想从我的strings.xml文件中获取数组 我已经设置了Spinners和Buttons。

<string-array name="angles">
    <item value="1">Radian</item>
    <item value="(Math.PI/3200)">Mil</item>
    <item value="(Math.PI/200)">Grad</item>
</string-array>

我设法得到了价值,但休息对我来说是一个难题:

public class parser {

public static void main(String argv[]) {

    try {

        File fXmlFile = new File("/home/frac/AndroidStudioProjects/UnitConverter/app/src/main/res/values/strings.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);


        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("item");



        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            String deger = nNode.getNodeValue();



            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println( eElement.getAttribute("value"));


            }




        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

   String[] angles= getResources().getStringArray(R.array.angles);
    for(i=0;i<angles.length;i++)
    {
      String str=angles[i];
      System.out.println(str);
    }