标签:
需求:有一个xml文件,包含省市县的信息(id,name,下属分划)
将xml文件信息解析到安卓下拉列表(spinner),三个下拉列表,第一个显示省名,第二个显示市名,第三个显示县名
要求选择第一个下拉列表的一个省名,相应市县信息跟随变化
方案分析:
1、因为要解析xml文件的全部信息,可以选择pull解析或者sax解析
2、有省、市、县等信息 则要有三个对应的实体类
xml文件 部分内容
1 - <p p_id="01"> 2 <pn>北京</pn> 3 - <c c_id="0101"> 4 <cn>北京</cn> 5 <d d_id="101010101">北京</d> 6 <d d_id="101010200">海淀</d> 7 <d d_id="101010300">朝阳</d> 8 <d d_id="101010400">顺义</d> 9 <d d_id="101010500">怀柔</d> 10 <d d_id="101010600">通州</d> 11 <d d_id="101010700">昌平</d> 12 <d d_id="101010800">延庆</d> 13 <d d_id="101010900">丰台</d> 14 <d d_id="101011000">石景山</d> 15 <d d_id="101011100">大兴</d> 16 <d d_id="101011200">房山</d> 17 <d d_id="101011300">密云</d> 18 <d d_id="101011400">门头沟</d> 19 <d d_id="101011500">平谷</d> 20 </c> 21 </p> 22 - <p p_id="02"> 23 <pn>上海</pn> 24 - <c c_id="0201"> 25 <cn>上海</cn> 26 <d d_id="101020100">上海</d> 27 <d d_id="101020200">闵行</d> 28 <d d_id="101020300">宝山</d> 29 <d d_id="101020500">嘉定</d> 30 <d d_id="101020600">浦东南汇</d> 31 <d d_id="101020700">金山</d> 32 <d d_id="101020800">青浦</d> 33 <d d_id="101020900">松江</d> 34 <d d_id="101021000">奉贤</d> 35 <d d_id="101021100">崇明</d> 36 <d d_id="101021200">徐家汇</d> 37 <d d_id="101021300">浦东</d> 38 </c> 39 </p>
省的信息有:id name List<City>
市的信息有:id name List<District>
县的信息有:id name
由此先创建三个对应省市县的实体类:
1 package instance; 2 3 import java.util.List; 4 5 public class Province { 6 private int id; 7 private String name; 8 private List<City> citys; 9 public Province() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 public Province(int id, String name, List<City> citys) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.citys = citys; 18 } 19 public int getId() { 20 return id; 21 } 22 public void setId(int id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public List<City> getCitys() { 32 return citys; 33 } 34 public void setCitys(List<City> citys) { 35 this.citys = citys; 36 } 37 @Override 38 public String toString() { 39 return name; 40 } 41 42 43 }
1 package instance; 2 3 import java.util.List; 4 5 public class City { 6 private int id; 7 private String name; 8 private List<District> districts; 9 public City() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 public City(int id, String name, List<District> districts) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.districts = districts; 18 } 19 public int getId() { 20 return id; 21 } 22 public void setId(int id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public List<District> getDistricts() { 32 return districts; 33 } 34 public void setDistricts(List<District> districts) { 35 this.districts = districts; 36 } 37 @Override 38 public String toString() { 39 return name; 40 } 41 42 43 44 }
1 package instance; 2 3 public class District { 4 private int id; 5 private String name; 6 public District() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public District(int id, String name) { 11 super(); 12 this.id = id; 13 this.name = name; 14 } 15 public int getId() { 16 return id; 17 } 18 public void setId(int id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 @Override 28 public String toString() { 29 return name; 30 } 31 32 33 34 }
然后写解析类,这里用pull解析,注意何时创建列表,何时创建对象,何时将列表作为成员属性加入到对应对象中
1 package tools; 2 3 import instance.City; 4 import instance.District; 5 import instance.Province; 6 7 import java.io.IOException; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import org.xmlpull.v1.XmlPullParser; 12 import org.xmlpull.v1.XmlPullParserException; 13 import org.xmlpull.v1.XmlPullParserFactory; 14 15 import com.example.pull.R; 16 17 import android.content.Context; 18 import android.content.res.Resources; 19 20 public class Pull { 21 //为了可以使用getResources()方法,需要传入上下文参数 22 private Context context; 23 public Pull(Context context){ 24 this.context = context; 25 } 26 27 public List<Province> pullparser() throws XmlPullParserException, IOException{ 28 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 29 XmlPullParser parser = factory.newPullParser(); 30 List<Province> provinces = null; 31 List<City> citys = null; 32 List<District> distrists =null; 33 Province province = null; 34 City city = null; 35 District distrist = null; 36 //得到xml文件 ,该文件放在res/raw/下 37 Resources resource = context.getResources(); 38 parser.setInput(resource.openRawResource(R.raw.citys_weather), "utf-8"); 39 40 //xml解析 41 int event = parser.getEventType(); 42 43 while(event!=XmlPullParser.END_DOCUMENT) 44 { 45 switch (event) { 46 //文件解析开始,需要创建一个集合类存放所有的省的信息 47 case XmlPullParser.START_DOCUMENT: 48 provinces = new ArrayList<Province>(); 49 break; 50 51 case XmlPullParser.START_TAG: 52 if("p".equals(parser.getName())) 53 { 54 //如果成立,则说明有一个新的省出现,则要创建一个新的省的对象,因为省有个存放该省所有市的集合,所以也要在这里创建集合对象 55 province = new Province(); 56 citys = new ArrayList<City>(); 57 int count = parser.getAttributeCount(); 58 for(int i=0;i<count;i++) 59 { 60 String key = parser.getAttributeName(i); 61 String value = parser.getAttributeValue(i); 62 if("p_id".equals(key)) 63 { 64 province.setId(Integer.parseInt(value)); 65 } 66 } 67 } 68 //给省名赋值 69 else if("pn".equals(parser.getName())) 70 { 71 province.setName(parser.nextText()); 72 } 73 else if("c".equals(parser.getName())) 74 { 75 //如果成立,则说明有一个新的市出现,则要创建一个新的市的对象,因为市有个存放该市所有县的集合,所以也要在这里创建县的集合对象 76 77 city = new City(); 78 distrists = new ArrayList<District>(); 79 int count = parser.getAttributeCount(); 80 for(int i=0;i<count;i++) 81 { 82 String key = parser.getAttributeName(i); 83 String value = parser.getAttributeValue(i); 84 if("c_id".equals(key)) 85 { 86 city.setId(Integer.parseInt(value)); 87 } 88 } 89 } 90 //给市名赋值 91 else if("cn".equals(parser.getName())) 92 { 93 city.setName(parser.nextText()); 94 } 95 else if("d".equals(parser.getName())) 96 { 97 98 //如果成立,则说明有一个新的县出现,则要创建一个新的县的对象, 99 100 distrist = new District(); 101 int count = parser.getAttributeCount(); 102 for(int i=0;i<count;i++) 103 { 104 String key = parser.getAttributeName(i); 105 String value = parser.getAttributeValue(i); 106 if("d_id".equals(key)) 107 { 108 distrist.setId(Integer.parseInt(value)); 109 } 110 } 111 112 distrist.setName(parser.nextText()); 113 //县的信息完成了 则需要把县加入到县集合中取 114 distrists.add(distrist); 115 } 116 break; 117 //解析结束标签 118 case XmlPullParser.END_TAG: 119 if("p".equals(parser.getName())) 120 { 121 //当"p"为结束标签时,说明该省结束了,则需要将该省的成员变量-市的集合放入省中 122 //并且将该省加入到存放省的集合中 123 province.setCitys(citys); 124 provinces.add(province); 125 } 126 else if("c".equals(parser.getName())) 127 { 128 //如上 129 city.setDistricts(distrists); 130 citys.add(city); 131 } 132 133 } 134 event = parser.next(); 135 136 } 137 //返回存放所有省的列表集合 138 return provinces; 139 } 140 }
解析步骤完成了,接下来需要和安卓下拉列表配合起来,将解析后的数据存放到下拉列表的适配器中
首先看布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <Spinner 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:id="@+id/spinner_province" 16 /> 17 18 <Spinner 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/spinner_city" 22 /> 23 24 <Spinner 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:id="@+id/spinner_district" 28 /> 29 30 31 32 </LinearLayout>
然后看Activity
1 package com.example.pull; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import org.xmlpull.v1.XmlPullParserException; 7 8 import tools.Pull; 9 import instance.City; 10 import instance.District; 11 import instance.Province; 12 import android.os.Bundle; 13 import android.app.Activity; 14 import android.view.Menu; 15 import android.view.View; 16 import android.widget.AdapterView; 17 import android.widget.AdapterView.OnItemClickListener; 18 import android.widget.AdapterView.OnItemSelectedListener; 19 import android.widget.ArrayAdapter; 20 import android.widget.Spinner; 21 22 public class MainActivity extends Activity { 23 private Spinner spinner_province; 24 private Spinner spinner_city; 25 private Spinner spinner_distrist; 26 private int city; 27 List<Province> list = null; 28 ArrayAdapter<Province> ada_province = null; 29 ArrayAdapter<City> ada_city=null; 30 ArrayAdapter<District> ada_district =null; 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 spinner_province = (Spinner) findViewById(R.id.spinner_province); 37 spinner_city = (Spinner) findViewById(R.id.spinner_city); 38 spinner_distrist = (Spinner) findViewById(R.id.spinner_district); 39 Pull pullparser = new Pull(this); 40 41 try { 42 //得到存放省的信息的集合列表 43 list = pullparser.pullparser(); 44 } catch (XmlPullParserException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } catch (IOException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 //将存放省名的集合作为第一个下拉列表的数据源,默认第一个省 52 ada_province = new ArrayAdapter<Province>(this, android.R.layout.simple_list_item_1,list); 53 spinner_province.setAdapter(ada_province); 54 //将存放市名的集合作为第二个下拉列表的数据源,默认第一个省的第一个市 55 ada_city = new ArrayAdapter<City>(this, android.R.layout.simple_list_item_1,list.get(0).getCitys()); 56 spinner_city.setAdapter(ada_city); 57 //将存放县名的集合作为第三个下拉列表的数据源,默认第一个省的第一个市的第一个县 58 ada_district = new ArrayAdapter<District>(this, android.R.layout.simple_list_item_1,list.get(0).getCitys().get(0).getDistricts()); 59 spinner_distrist.setAdapter(ada_district); 60 61 //显示省名信息的下拉列表的响应事件,当第一个列表项更换时,修改第二个下拉列表的适配器的数据源,对应为第一个省的所有市 62 spinner_province.setOnItemSelectedListener(new OnItemSelectedListener() { 63 64 @Override 65 public void onItemSelected(AdapterView<?> arg0, View arg1, 66 int arg2, long arg3) { 67 // TODO Auto-generated method stub 68 city = arg2; 69 ada_city = new ArrayAdapter<City>(MainActivity.this, android.R.layout.simple_list_item_1,list.get(arg2).getCitys()); 70 spinner_city.setAdapter(ada_city); 71 } 72 73 @Override 74 public void onNothingSelected(AdapterView<?> arg0) { 75 // TODO Auto-generated method stub 76 77 } 78 }); 79 //同上 80 spinner_city.setOnItemSelectedListener(new OnItemSelectedListener() { 81 82 @Override 83 public void onItemSelected(AdapterView<?> arg0, View arg1, 84 int arg2, long arg3) { 85 // TODO Auto-generated method stub 86 ada_district = new ArrayAdapter<District>(MainActivity.this, android.R.layout.simple_list_item_1,list.get(city).getCitys().get(arg2).getDistricts()); 87 spinner_distrist.setAdapter(ada_district); 88 } 89 90 @Override 91 public void onNothingSelected(AdapterView<?> arg0) { 92 // TODO Auto-generated method stub 93 94 } 95 }); 96 97 } 98 99 100 101 }
效果图:
标签:
原文地址:http://www.cnblogs.com/xqxacm/p/4774131.html