<span style="font-size:18px;">要想实现省市区三联动首先要有 省市区的xml资源我用的是android studio,我把下载好的资源放到了res下新建的一个叫xml的文件夹中位置:</span>
<span style="font-size:18px;">用pull方式解析xml资源:</span>
<span style="font-size:18px;">//解析省</span>
<span style="background-color: rgb(255, 255, 255); font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"> public void Getprovince(){
XmlResourceParser xrp=getResources().getXml(R.xml.provinces);//获取res下xml中的provinces.xml资源
try{
while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
if(xrp.getEventType()==XmlResourceParser.START_TAG){
String name=xrp.getName();
if(name.equals("Province")){
province.add(xrp.getAttributeValue(1));
}else if(xrp.getEventType()==XmlResourceParser.END_TAG){
}else if(xrp.getEventType()==XmlResourceParser.TEXT){
}
}
xrp.next();
}
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
//解析市
<pre name="code" class="java">public void Getcity(){
String flag="1";
String flag1="1";
XmlResourceParser xrp=getResources().getXml(R.xml.cities);<span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif;">//获取res下xml中的citys.xml资源</span>
try{
List city=new ArrayList();
List id=new ArrayList();
while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
if(xrp.getEventType()==XmlResourceParser.START_TAG){
String name=xrp.getName();
if(name.equals("City")){
if(!flag1.equals(xrp.getAttributeValue(2))){
ID.put(flag1,id);
id=new ArrayList();
flag1=xrp.getAttributeValue(2);
}
id.add(xrp.getAttributeValue(0));
if(!flag.equals(xrp.getAttributeValue(2))){
City.put(flag,city);
city=new ArrayList();
flag=xrp.getAttributeValue(2);
}
city.add(xrp.getAttributeValue(1));
}else if(xrp.getEventType()==XmlResourceParser.END_TAG){
}else if(xrp.getEventType()==XmlResourceParser.TEXT){
}
}
xrp.next();
}
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
//解析区
<pre name="code" class="java">public void Getdistric(){
String flag="1";
XmlResourceParser xrp=getResources().getXml(R.xml.districts);<span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif;">//获取res下xml中的districts.xml资源</span>
try{
List district=new ArrayList();
while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
if(xrp.getEventType()==XmlResourceParser.START_TAG){
String name=xrp.getName();
if(name.equals("District")){
if(!flag.equals(xrp.getAttributeValue(2))){
Distric.put(flag,district);
district=new ArrayList();
flag=xrp.getAttributeValue(2);
}
district.add(xrp.getAttributeValue(1));
}else if(xrp.getEventType()==XmlResourceParser.END_TAG){
}else if(xrp.getEventType()==XmlResourceParser.TEXT){
}
}
xrp.next();
}
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
这当中对获取的地点要进行保存,这时候就要考虑到用什么数据结构才能实现省市区之间有关联呢?
<pre name="code" class="java"> List province=new ArrayList();
String flag;
Map<String,List> City=new HashMap<>();
Map<String,List> Distric=new HashMap<>();
Map<String,List> ID=new HashMap<>();
通过上面几个函数把省市区信息都保存到定义的这几个结构里面了。到此就剩下下拉框的创建并开始调用这几个结构啦。
这是layout里面的布局文件 <a target=_blank href="http://blog.csdn.net/greenappple/article/details/7046947">参考</a>
<pre style="background-color: rgb(255, 255, 255); font-family: 宋体; font-size: 9pt;"><<span style="color:#000080;"><strong>LinearLayout </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"40dip"
</strong></span><span style="color:#008000;"><strong> </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"fill_parent"
</strong></span><span style="color:#008000;"><strong> </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/LinearLayoutArea" </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:orientation=</strong></span><span style="color:#008000;"><strong>"horizontal"</strong></span>>
<<span style="color:#000080;"><strong>Spinner </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/spprovince" </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"100dip"
</strong></span><span style="color:#008000;"><strong> </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"</strong></span>>
</<span style="color:#000080;"><strong>Spinner</strong></span>>
<<span style="color:#000080;"><strong>Spinner </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/spcity" </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"110dip"
</strong></span><span style="color:#008000;"><strong> </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"</strong></span>>
</<span style="color:#000080;"><strong>Spinner</strong></span>>
<<span style="color:#000080;"><strong>Spinner </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/sparea" </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"90dip"
</strong></span><span style="color:#008000;"><strong> </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"</strong></span>>
</<span style="color:#000080;"><strong>Spinner</strong></span>>
</<span style="color:#000080;"><strong>LinearLayout</strong></span>>
好啦,开始调用:参考
spprovince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { adapter2 = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_spinner_item, City.get(String.valueOf(position))); spcity.setAdapter(adapter2); flag = String.valueOf(position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); spcity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { adapter3 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, Distric.get(ID.get(flag).get(position))); sparea.setAdapter(adapter3); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); sparea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { txt.setText(spprovince.getSelectedItem().toString() + spcity.getSelectedItem().toString() + sparea.getSelectedItem().toString()); } @Override public void onNothingSelected(AdapterView<?> parent) { } });总体就是这样的。