猿问

有没有办法将存储在 XML 中的值数组读取到类型化的 ArrayList 中?

我刚开始使用 Android Studio 和 Java。我想读入一个文件(可以是任何文件格式),该文件存储一对字符串 + 长的数组。然后我想将该数组输入到一个类中(假设这只是一个简单的 for 循环,通过我从 XML 中读取的内容构造元素) - QUEestion 更多的是关于读取数据。

我已经摆弄了很多 res/values/ 中的文件,但还没有找到一种方法来为一个项目分配两个值。如果我可以调用 xml[0].string 和 xml[0].long,那对于我打算做的事情来说已经足够了。


慕哥9229398
浏览 169回答 2
2回答

慕妹3242003

您可以在资源中设置两个数组以在最后制作一个资源/值/arrays.xml<?xml version="1.0" encoding="utf-8"?><resources>&nbsp; &nbsp; <integer-array name="integer_array">&nbsp; &nbsp; &nbsp; &nbsp; <item>1</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>2</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>3</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>4</item>&nbsp; &nbsp; </integer-array>&nbsp; &nbsp; <string-array name="string_array">&nbsp; &nbsp; &nbsp; &nbsp; <item>string 1</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>string 2</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>string 3</item>&nbsp; &nbsp; &nbsp; &nbsp; <item>string 4</item>&nbsp; &nbsp; </string-array></resources>在你的课上Resources res = getResources();int[] ints = res.getIntArray(R.array.integer_array);String[] strings = res.getStringArray(R.array.string_array);//add them to your object arrayArrayList<YourObject> list = new ArrayList();int i = 0;while(i < ints.length){&nbsp; &nbsp; //make sure the two lists are the same size&nbsp; &nbsp; list.add(new YourObject(ints[i], strings[i]));&nbsp; &nbsp; i++;}我不确定你是否可以long用这个获得物品,但整数应该可以完成这项工作

慕姐8265434

试试这个,示例:&nbsp;把它放在 rec/values/arrays.xml 中的 arrays.xml 中<string-array&nbsp;name="array_gender"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item>Select&nbsp;Gender</item> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item>Male</item> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item>Female</item> &nbsp;&nbsp;&nbsp;&nbsp;</string-array>用法List<String>&nbsp;myArrayList&nbsp;=&nbsp;Arrays.asList(getResources().getStringArray(R.array.array_gender));
随时随地看视频慕课网APP

相关分类

Java
我要回答