实现功能:解析AddressTable.xml,里面存放的是中国的省、市、县、镇,通过省、市查看对应的、县市,然后在查询下一级。
<China>
<firstAddress AddressCode="1100000000" AddressName="北京" AddressDisabled="N">
<secondAddress AddressCode="1101000000" AddressName="市辖区" AddressDisabled="N">
<thirdAddress AddressCode="1101010000" AddressName="东城区1" AddressDisabled="N" />
<thirdAddress AddressCode="1101020000" AddressName="西城区" AddressDisabled="N" />
<thirdAddress AddressCode="1101030000" AddressName="崇文区" AddressDisabled="N" />
<thirdAddress AddressCode="1101040000" AddressName="宣武区" AddressDisabled="N" />
<thirdAddress AddressCode="1101050000" AddressName="朝阳区" AddressDisabled="N" />
<thirdAddress AddressCode="1101060000" AddressName="丰台区" AddressDisabled="N" />
<thirdAddress AddressCode="1101070000" AddressName="石景山区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101080000" AddressName="海淀区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101090000" AddressName="门头沟区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101110000" AddressName="房山区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101120000" AddressName="通州区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101130000" AddressName="顺义区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101140000" AddressName="昌平区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101150000" AddressName="大兴区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101160000" AddressName="怀柔区" AddressDisabled="Y" />
<thirdAddress AddressCode="1101170000" AddressName="平谷区" AddressDisabled="Y" />
</secondAddress>
<secondAddress AddressCode="1102000000" AddressName="县" AddressDisabled="N">
<thirdAddress AddressCode="1102280000" AddressName="密云" AddressDisabled="N" />
<thirdAddress AddressCode="1102290000" AddressName="延庆县" AddressDisabled="N" />
</secondAddress>
</firstAddress>
部分文件展示,解析XML文件的核心代码如下:
public void getChinaAddresssFromXml(Context context, String fileName) {
Node first = null;
Node second = null;
Node third = null;
try {
// 首先找到xml文件
factory = DocumentBuilderFactory.newInstance();
// 找到xml,并加载文档
builder = factory.newDocumentBuilder();
inputStream = context.getResources().getAssets().open(fileName);
document = builder.parse(inputStream);
// 找到根Element
Element root = document.getDocumentElement();
// firstAddress
NodeList nodelist = root.getChildNodes();
for (int i = 0; i < nodelist.getLength(); i++) {
first = nodelist.item(i);
// firstAddress
if (first.getNodeType() == Node.ELEMENT_NODE) {
if (first.getNodeName().equals("firstAddress")) {
NamedNodeMap namednodemap = first.getAttributes();
String AddressCode = namednodemap.getNamedItem(
"AddressCode").getNodeValue();
String AddressName = namednodemap.getNamedItem(
"AddressName").getNodeValue();
String AddressDisabled = namednodemap.getNamedItem(
"AddressDisabled").getNodeValue();
province.add(new ChinaAddress(AddressCode, AddressName,
AddressDisabled));
}
}
// secondAddress
for (second = first.getFirstChild(); second != null; second = second
.getNextSibling()) {
if (second.getNodeType() == Node.ELEMENT_NODE) {
if (second.getNodeName().equals("secondAddress")) {
NamedNodeMap namednodemap = second.getAttributes();
String AddressCode = namednodemap.getNamedItem(
"AddressCode").getNodeValue();
String AddressName = namednodemap.getNamedItem(
"AddressName").getNodeValue();
String AddressDisabled = namednodemap.getNamedItem(
"AddressDisabled").getNodeValue();
county.add(new ChinaAddress(AddressCode,
AddressName, AddressDisabled));
}
}
// thirdAddress
for (third = second.getFirstChild(); third != null; third = third
.getNextSibling()) {
if (third.getNodeType() == Node.ELEMENT_NODE) {
if (third.getNodeName().equals("thirdAddress")) {
NamedNodeMap namednodemap = third
.getAttributes();
String AddressCode = namednodemap.getNamedItem(
"AddressCode").getNodeValue();
String AddressName = namednodemap.getNamedItem(
"AddressName").getNodeValue();
String AddressDisabled = namednodemap
.getNamedItem("AddressDisabled")
.getNodeValue();
town.add(new ChinaAddress(AddressCode,
AddressName, AddressDisabled));
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
该XML文件里主要有三个标签firstAddress,secondAddress,thirdAddress,通过for循环调用getNo的Name()方法进行判断,然后调用getAttributes()获取属性,接着再调用getNamedItem( “AddressCode”).getNodeValue()获取对应属性名的值即可。
使用ListView展示部分,具体看代码,并不难。主要是解析XML获取到想要的值填充到
private ArrayList<ChinaAddress> province = new ArrayList<ChinaAddress>();
private ArrayList<ChinaAddress> county = new ArrayList<ChinaAddress>();
private ArrayList<ChinaAddress> town = new ArrayList<ChinaAddress>();
然后再映射到对应的ListView即可,关键部分是就是ListView的监听,获取对应项的AddressCode,并用于判断下一个ListView应该填充哪些内容。
listview.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>) parent
.getItemAtPosition(position);
String name = (String) map.get("AddressName");
AddressCode = (String) map.get("AddressCode");
String keyCode = AddressCode.substring(0, 2);
// Toast.makeText(MainActivity.this, name + keyCode, 0).show();
HashMap<String, Object> countymap;
ChinaAddress c;
countyDataList.clear();
for (int i = 0; i < county.size(); i++) {
c = new ChinaAddress();
c = county.get(i);
// 根据代码获取选区省市对应的县、镇
if (c.getAddressCode().substring(0, 2).equals(keyCode)) {
countymap = new HashMap<String, Object>();
countymap.put("AddressName", c.getAddressName());
countymap.put("AddressCode", c.getAddressCode());
countymap.put("AddressDisabled", c.getAddressDisabled());
countyDataList.add(countymap);
}
}
// 设置countyDataList共享
((MyAppData) getApplication())
.setCountyDataList(countyDataList);
Intent intent = new Intent();
intent.putExtra("TITLE", name);
intent.setClass(MainActivity.this, CountyActivity.class);
startActivity(intent);
}
});
基本上差不多,代码很简单,下载源代码自己查看即可。对了,搜素功能暂未实现,有兴趣可以自己试试。
http://pan.baidu.com/s/1ntt4RA5点击打开链接
运行截图:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/116903.html