天天看點

省市區三級關聯之-------spinner

效果圖:

省市區三級關聯之-------spinner

主要代碼子產品:

//..............省市區關聯.......

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<String> provincesName = new ArrayList<String>();

for (Province province : provinces) {

provincesName.add(province.getName());

}

List<String> citysName = new ArrayList<String>();

for (City city : provinces.get(0).getCityList()) {

citysName.add(city.getName());

}

List<String> areasName= new ArrayList<String>();

for (Area area : provinces.get(0).getCityList().get(0).getAreas()) {

areasName.add(area.getName());

}

adapter_province = new ArrayAdapter<String>(this,R.layout.myspinner, provincesName);

sp_province.setAdapter(adapter_province);

sp_province.setSelection(0, true);

adapter_city = new ArrayAdapter<String>(this,R.layout.myspinner,citysName);

sp_city.setAdapter(adapter_city);

sp_city.setSelection(0, true);

adapter_area = new ArrayAdapter<String>(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<String> scitysName =new ArrayList<String>();

List<City> cityList = province.getCityList();

for (City c : cityList) {

scitysName.add(c.getName());

}

adapter_city = new ArrayAdapter<String>(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<String> sareasName = new ArrayList<String>(); 

for (Area a : city.getAreas()) {

sareasName.add(a.getName());

}

// adapter_area = new ArrayAdapter<String>(RepairActivity.this,android.R.layout.simple_list_item_1,sareasName);

adapter_area = new ArrayAdapter<String>(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 

繼續閱讀