---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
今天要实现省份,城市。区的选择,到网络上百度了一下。发现非常多实现都是用的 sqlite 数据库,可是我这边为了保证和 ios 那边数据统一,仅仅要用那边的 plist 文件。也就是我们常说的 xml 文件。就想找一个网络上读取 xml 文件来实现的。发现基本没有,就算有。也是把所有的资源所实用数组列举出来,而不是实时读取。
网络上的实现:
1. 利用 sqlite 訪问 db 数据库。
2. 列举出全部的 xml 资源。
我的实现:
1. 动态读取 xml 文件。
2. 须要什么读取什么,假设获取了想要的资源。则停止读取。
3. 异步读取。
改进:
1. 读取的资源没有进行缓存。这个看详细需求吧。
2. 代码实现算法能够进行优化。
3.xml 文件结构能够优化,这个是 ios 那边直接给我的资源。可是看着非常无语,city.xml 比較少,就直接手动改动了一下,area.xml 文件比較多,就没改了。
建议:
这个博客仅仅是给大家提供了一个实现的思路,大家不好照搬,最好依据自己的需求来详细实现,最好是换一个结构更加合理的 xml 文件,在实现一个比較优秀的算法。切记,本博客仅仅是提供一个思路!~。~
city.xml 的基本结构
- <?xml version="1.0" encoding="UTF-8" ?>
- <array>
- <dict>
- <key>
- state
- </key>
- <string>
- 北京
- </string>
- <key>
- cities
- </key>
- <array>
- <string>
- 通州
- </string>
- <string>
- 房山
- </string>
- <string>
- 昌平
- </string>
- <string>
- 顺义
- </string>
- <string>
- 怀柔
- </string>
- <string>
- 大兴
- </string>
- <string>
- 密云
- </string>
- <string>
- 平谷
- </string>
- <string>
- 延庆
- </string>
- <string>
- 东城
- </string>
- <string>
- 崇文
- </string>
- <string>
- 西城
- </string>
- <string>
- 朝阳
- </string>
- <string>
- 宣武
- </string>
- <string>
- 石景山
- </string>
- <string>
- 丰台
- </string>
- <string>
- 门头沟
- </string>
- <string>
- 海淀
- </string>
- </array>
- </dict>
- ....
- </array>
1. 读取城市的代码
- /**
- * 获取省份数据列表
- */
- private List<String> getProvince(){
- //结果存储
- List<String> resultList = new ArrayList<String>();
- //标记是否读取下一个节点的内容
- boolean nextRead = false;
- try {
- XmlPullParser xrpCity = Xml.newPullParser();
- xrpCity.setInput(getAssets().open("city.xml"), "UTF-8");
- while (xrpCity.getEventType() != XmlPullParser.END_DOCUMENT) {
- //假设是開始标签
- if (xrpCity.getEventType() == XmlPullParser.START_TAG) {
- //获取标签名称
- String name = xrpCity.getName();
- //推断标签名称是否等于friend
- if("key".equals(name)){
- xrpCity.next();
- String value = xrpCity.getText();
- if("state".equals(value)){
- nextRead = true;
- }
- }else if("string".equals(name) && nextRead){
- xrpCity.next();
- resultList.add(xrpCity.getText());
- nextRead = false;
- }
- }
- //下一个标签
- xrpCity.next();
- }
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- }
- return resultList;
- }
2. 读取城市的代码
- /**
- * 获取城市列表
- */
- private List<String> getCity(String provinceName){
- //结果存储
- List<String> resultList = new ArrayList<String>();
- //标记是否读取下一个节点的内容
- boolean nextRead = false;
- //读取城市节点内容的标记
- boolean readCity = false;
- try {
- XmlPullParser xrpCity = Xml.newPullParser();
- xrpCity.setInput(getAssets().open("city.xml"), "UTF-8");
- while (xrpCity.getEventType() != XmlPullParser.END_DOCUMENT) {
- //假设是開始标签
- if (xrpCity.getEventType() == XmlPullParser.START_TAG) {
- //获取标签名称
- String name = xrpCity.getName();
- if("key".equals(name)){
- xrpCity.next();
- String value = xrpCity.getText();
- if("state".equals(value)){
- nextRead = true;
- if(readCity){
- break;
- }
- readCity = false;
- }
- }else if("string".equals(name) && nextRead){
- xrpCity.next();
- nextRead = false;
- if(provinceName.equals(xrpCity.getText())){
- readCity = true;
- }
- }else if("string".equals(name) && readCity){
- xrpCity.next();
- resultList.add(xrpCity.getText());
- }
- }
- //下一个标签
- xrpCity.next();
- }
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- }
- return resultList;
- }
area.xml 资源的基本结构
- <?xml version="1.0" encoding="UTF-8" ?>
- <array>
- ....
- <dict>
- <key>
- areas
- </key>
- <array>
- <string>
- 连山壮族瑶族自治县
- </string>
- <string>
- 阳山县
- </string>
- <string>
- 佛冈县
- </string>
- <string>
- 清城区
- </string>
- <string>
- 连南瑶族自治县
- </string>
- <string>
- 清新县
- </string>
- <string>
- 英德市
- </string>
- <string>
- 连州市
- </string>
- </array>
- <key>
- city
- </key>
- <string>
- 清远
- </string>
- </dict>
- .....
- </array>
1. 读取区的代码
- /**
- * 获取区列表
- */
- private List<String> getRegion(String citName){
- //结果存储
- List<String> resultList = new ArrayList<String>();
- //标记是否读取下一个节点的内容
- boolean nextRead = false;
- //读取城市节点内容的标记
- boolean readCity = false;
- try {
- XmlPullParser xrpCity = Xml.newPullParser();
- xrpCity.setInput(getAssets().open("area.xml"), "UTF-8");
- while (xrpCity.getEventType() != XmlPullParser.END_DOCUMENT) {
- //假设是開始标签
- if (xrpCity.getEventType() == XmlPullParser.START_TAG) {
- //获取标签名称
- String name = xrpCity.getName();
- if("key".equals(name)){
- xrpCity.next();
- String value = xrpCity.getText();
- if("areas".equals(value)){
- nextRead = true;
- readCity = false;
- }else if("city".equals(value)){
- readCity = true;
- nextRead = false;
- }
- }else if("string".equals(name) && nextRead){
- xrpCity.next();
- resultList.add(xrpCity.getText());
- }else if("string".equals(name) && readCity){
- xrpCity.next();
- if(citName.equals(xrpCity.getText())){
- break;
- }else{
- resultList.clear();
- }
- }
- }
- //下一个标签
- xrpCity.next();
- }
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- resultList = null;
- }
- return resultList;
- }
上面就是基本的实现,待会把功能做个小项目,传到 csdn 之后。在给出 demo 地址,和资源。
地址例如以下:http://download.csdn.net/detail/jiguangcanhen/8152421
该项目为 ant 架构,gradle 架构的懒得弄了,自己搞定吧。
来源: http://www.bubuko.com/infodetail-2045914.html