我试图在res/raw/data.xml
中制作expandableListView数据,但我不知道如何连接它。在此之前,我正在使用来自以下活动的直接数据:
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Basic");
listDataHeader.add("Data Types");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Introduction");
top250.add("Data Types");
top250.add("Schema");
top250.add("Use in Ajax");
top250.add("Examples");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Overview");
nowShowing.add("Array");
nowShowing.add("Boolean");
nowShowing.add("Object");
nowShowing.add("Number");
nowShowing.add("String");
nowShowing.add("False");
nowShowing.add("True");
nowShowing.add("Null");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
}
}
我想添加res/raw/data.xml
的数据。这是data.xml
代码:
<?xml version="1.0" encoding="UTF-8"?>
<entries>
<section name="Basics">
<entry id="1">
<name>Introduction</name>
<htmlfilename>basics_intro</htmlfilename>
</entry>
<entry id="2">
<name>Data Types</name>
<htmlfilename>basics_datatypes</htmlfilename>
</entry>
<entry id="3">
<name>Schema</name>
<htmlfilename>basics_schema</htmlfilename>
</entry>
<entry id="3">
<name>Use in Ajax</name>
<htmlfilename>basics_ajaxuse</htmlfilename>
</entry>
<entry id="4">
<name>Cheat Sheet</name>
<htmlfilename>basics_cheatsheet</htmlfilename>
</entry>
<entry id="4">
<name>Examples</name>
<htmlfilename>basics_examples</htmlfilename>
</entry>
</section>
<section name="Data Types">
<entry id="5">
<name>Overview</name>
<htmlfilename>datatypes_overview</htmlfilename>
</entry>
<entry id="6">
<name>Array</name>
<htmlfilename>datatypes_array</htmlfilename>
</entry>
<entry id="7">
<name>Boolean</name>
<htmlfilename>datatypes_boolean</htmlfilename>
</entry>
<entry id="8">
<name>Object</name>
<htmlfilename>datatypes_object</htmlfilename>
</entry>
<entry id="9">
<name>Number</name>
<htmlfilename>datatypes_number</htmlfilename>
</entry>
<entry id="10">
<name>String</name>
<htmlfilename>datatypes_string</htmlfilename>
</entry>
<entry id="11">
<name>true</name>
<htmlfilename>datatypes_true</htmlfilename>
</entry>
<entry id="11">
<name>false</name>
<htmlfilename>datatypes_false</htmlfilename>
</entry>
<entry id="11">
<name>null</name>
<htmlfilename>datatypes_null</htmlfilename>
</entry>
</section>
</entries>
任何人都可以帮助我吗?
答案 0 :(得分:0)
目前,您正在listDataHeader
和listDataChild
中静态添加数据。从xml文件中将ArrayList和HashMap中的数据更改为prepareListData()
方法:
1。将data.xml
文件移至assets
目录,而不是res
。
2。使用AssetManager
从资产中读取InputStream
文件:
AssetManager assetManager = YourAcivityClassName.this.getAssets(); InputStream inputStream = assetManager.open("data.xml");
3. 使用SAX
或DOM
解析器解析xml以获取ArrayList和HashMap中的所有节点数据。请参阅以下教程,使用SAXParser
来传递xml:
Android parse XML file from Assets or internal/external storage