省市区三级联动之-------spinner

效果图:省市区三级联动之-------spinner_第1张图片


主要代码模块:


//..............省市区联动.......


sp_province = (Spinner) findViewById(R.id.sp_province);//省
sp_city = (Spinner) findViewById(R.id.sp_city);//市
sp_area = (Spinner) findViewById(R.id.sp_area);//区

provinces = getProvinces();
province = provinces.get(0);
List provincesName = new ArrayList();

for (Province province : provinces) {
provincesName.add(province.getName());
}

List citysName = new ArrayList();
for (City city : provinces.get(0).getCityList()) {
citysName.add(city.getName());
}


List areasName= new ArrayList();
for (Area area : provinces.get(0).getCityList().get(0).getAreas()) {
areasName.add(area.getName());
}

adapter_province = new ArrayAdapter(this,R.layout.myspinner, provincesName);
sp_province.setAdapter(adapter_province);
sp_province.setSelection(0, true);

adapter_city = new ArrayAdapter(this,R.layout.myspinner,citysName);
sp_city.setAdapter(adapter_city);
sp_city.setSelection(0, true);

adapter_area = new ArrayAdapter(this, R.layout.myspinner,areasName);
sp_area.setAdapter(adapter_area);
sp_area.setSelection(0, true);

//选择省
sp_province.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView parent, View view,
int position, long id) {

province = provinces.get(position);
citys = province.getCityList();

strProvince = sp_province.getSelectedItem().toString();

List scitysName =new ArrayList();
List cityList = province.getCityList();
for (City c : cityList) {
scitysName.add(c.getName());
}

adapter_city = new ArrayAdapter(AddAddressActivity.this,
R.layout.myspinner, scitysName);

sp_city.setAdapter(adapter_city);

}


@Override
public void onNothingSelected(AdapterView parent) {


}
});

//选择市
sp_city.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView parent, View view,
int position, long id) {

   city = province.getCityList().get(position);
   
   strCity = sp_city.getSelectedItem().toString();
   
   et_address_display.setText(strProvince+"_"+strCity);
   
List sareasName = new ArrayList(); 
for (Area a : city.getAreas()) {
sareasName.add(a.getName());
}

// adapter_area = new ArrayAdapter(RepairActivity.this,android.R.layout.simple_list_item_1,sareasName);
adapter_area = new ArrayAdapter(AddAddressActivity.this,R.layout.myspinner,sareasName);
sp_area.setAdapter(adapter_area);

}


@Override
public void onNothingSelected(AdapterView parent) {

}
});

//选择区
sp_area.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView parent, View view,
int position, long id) {

area = city.getDistrictList().get(position);

strArea = sp_area.getSelectedItem().toString();

if(flag){

et_address_display.setText(strProvince+"_"+strCity+"-"+strArea);
}else{
et_address_display.setText("");
}

}


@Override
public void onNothingSelected(AdapterView arg0){
// TODO Auto-generated method stub
}
});

//.....................

如果想要Demo可以下载es运行就可以了:http://download.csdn.net/detail/qq_26576639/9513863 

你可能感兴趣的:(功能)