spinner 的三级联动用法
需求:将省,市,县从xml文档中解析出来,并展示到spinner控件中,当选中省级spinner时,相应的市级和县级会有相应的变化,选了省级以后再选市级,县级也会有相应的变化
方法:采用adapter链接数据源和控件,采用pull解析解析出数据,使用onItemSelectetListenner监听spinner的变化
(1)以下是xml文件中的部分代码,这里只做个样本
(2)下面便是关键代码了
package com.example.day04spinner041;
import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.location.Address;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Spinner spprovince,spcity,spviLlage;
private ArrayList
private ArrayAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spprovince=(Spinner) findViewById(R.id.spprovince);
spcity=(Spinner) findViewById(R.id.spcity);
spviLlage=(Spinner) findViewById(R.id.spvillage);
getProvince();
provinceaddpter=new ArrayAdapter
cityadapter=new ArrayAdapter
villageadapter=new ArrayAdapter
spprovince.setAdapter(provinceaddpter);
spcity.setAdapter(cityadapter);
spviLlage.setAdapter(villageadapter);
//spinner的监听事件
spcity.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
String cid=cidlist.get(position);
getVillage(cid);
villageadapter.clear();
villageadapter.addAll(vnamelist);
}
@Override
public void onNothingSelected(AdapterView> parent) {
// TODO Auto-generated method stub
}
});
spprovince.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
String pid=pidlist.get(position);
getCity(pid);
cityadapter.clear();
cityadapter.addAll(cnamelist);
getVillage(cidlist.get(position));
villageadapter.clear();
villageadapter.addAll(vnamelist);
}
@Override
public void onNothingSelected(AdapterView> parent) {
// TODO Auto-generated method stub
}
});
}
//解析省级数据(xml解析)
public void getProvince() {
XmlResourceParser parser=getResources().getXml(R.xml.cities);
try {
int type=parser.getEventType();
while(type!=1){
String tag=parser.getName();
switch(type){
case 0:
pidlist=new ArrayList
pnamelist=new ArrayList
break;
case 2:
if("p".equals(tag)){
pidlist.add(parser.getAttributeValue(0));
}
if("pn".equals(tag)){
pnamelist.add(parser.nextText());
}
break;
}
type=parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//解析市级数据
public void getCity(String pid) {
XmlPullParser parser=getResources().getXml(R.xml.cities);
try {
int type=parser.getEventType();
while(type!=1){
String tag=parser.getName();
switch(type){
case 0:
cidlist=new ArrayList
cnamelist=new ArrayList
break;
case 2:
if("c".equals(tag)){
String cid=parser.getAttributeValue(0);
if(cid.startsWith(pid)){
cidlist.add(cid);
parser.nextTag();
cnamelist.add(parser.nextText());
}
}
break;
}
type=parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//解析县级数据
public void getVillage(String cid) {
XmlPullParser parser=getResources().getXml(R.xml.cities);
try {
int type=parser.getEventType();
while(type!=1){
String tag=parser.getName();
switch (type) {
case 0:
vidlist=new ArrayList
vnamelist=new ArrayList
break;
case 2:
if("d".equals(tag)){
String vid=parser.getAttributeValue(0);
if(vid.contains(cid)){
vidlist.add(vid);
vnamelist.add(parser.nextText());
}
}
break;
}
type=parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}