先来张效果图吧,
在网上也有很多demo,点击按钮,弹出对话框选择省市区,直接设置进去,类似ios的省市区三联动。但客户需要以上样式的,么得办法,只能一点点撸。
布局文件跳过,直接上代码
一、省市区json文件解析
省市区json文件,放在assets文件夹中,就不用从服务器拉取。
1、省市区json文件解析:
String json = getJson(); try { if (json != null && json.length() > 0) { getJsonData(json); } } catch (Exception e) { e.printStackTrace(); }
/** * 读取json数据源 */ public String getJson() { AssetManager assetManager = this.getAssets(); StringBuilder stringBuilder = new StringBuilder(); try { BufferedReader bd = new BufferedReader(new InputStreamReader(assetManager.open("area.json"))); String line; while ((line = bd.readLine()) != null) { stringBuilder.append(line); } } catch (IOException e) { e.printStackTrace(); } return stringBuilder.toString(); }
2、解析json文件
/** * 解析储存数据 */ private void getJsonData(String json) { try { JSONArray jsonArray = new JSONArray(json); ProvinceArea provinceArea; for (int i = 0; i < jsonArray.length(); i++) { provinceArea = new ProvinceArea(); JSONObject js = (JSONObject) jsonArray.get(i); String value = js.getString("value"); String text = js.getString("text"); JSONArray cityJson = js.getJSONArray("children"); cityData = new ArrayList<CityArea>(); CityArea cityArea; for (int j = 0; j < cityJson.length(); j++) { cityArea = new CityArea(); JSONObject cityJo = (JSONObject) cityJson.get(j); String cityValue = cityJo.getString("value"); String cityName = cityJo.getString("text"); JSONArray countryArray = cityJo.getJSONArray("children"); countryData = new ArrayList<CountryArea>(); CountryArea countryArea; for (int m = 0; m < countryArray.length(); m++) { countryArea = new CountryArea(); JSONObject countryJo = (JSONObject) countryArray.get(m); String countryValue = countryJo.getString("value"); String countryName = countryJo.getString("text"); countryArea.setName(countryName); countryArea.setValue(countryValue); countryData.add(countryArea); } cityArea.setCityName(cityName); cityArea.setCityValue(cityValue); cityArea.setCountryList(countryData); cityData.add(cityArea); } provinceArea.setCityList(cityData); provinceArea.setProvinceName(text); provinceArea.setProvinceValue(value); pData.add(provinceArea); provinceCode.add(value); provinceName.add(text); } } catch (JSONException e) { e.printStackTrace(); } }
3、将数据分别存入对应的集合中,然后设置适配器
adapter01 = new ArrayAdapter<ProvinceArea>(this,android.R.layout.simple_spinner_dropdown_item, pData); spinnerProvince.setAdapter(adapter01); spinnerProvince.setSelection(0, true); provence = pData.get(0); //默认选中第一项,防止在获取城市id时候为空(省市区默认都选择第一项,不点击spinner,选择城市下标时会出现nullpoint) adapter02 = new ArrayAdapter<CityArea>(this, android.R.layout.simple_spinner_dropdown_item, pData.get(0).getCityList()); spinnerCity.setAdapter(adapter02); spinnerCity.setSelection(0, true); city = pData.get(0).getCityList().get(0);//默认选中第一项,防止在获取城市id时候为空 adapter03 = new ArrayAdapter<CountryArea>(this, android.R.layout.simple_spinner_dropdown_item, pData.get(0).getCityList().get(0).getCountryList()); spinnerArea.setAdapter(adapter03); spinnerArea.setSelection(0, true); country = pData.get(0).getCityList().get(0).getCountryList().get(0);//默认选中第一项,防止在获取城市id时候为空 spinnerProvince.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // // // 设置选中之后的字体和颜色 // if(view instanceof TextView){ // ((TextView)view).setTextColor(Color.RED); // ((TextView)view).setTextSize(12f); // } adapter02 = new ArrayAdapter<CityArea>(RegisterActivity.this, android.R.layout.simple_spinner_dropdown_item, pData.get(position).getCityList()); spinnerCity.setAdapter(adapter02); provence = pData.get(position); } @Override public void onNothingSelected(AdapterView<?> parent) {} }); spinnerCity.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { adapter03 = new ArrayAdapter<CountryArea>(RegisterActivity.this, android.R.layout.simple_spinner_dropdown_item, provence.getCityList().get(position).getCountryList()); spinnerArea.setAdapter(adapter03); city = provence.getCityList().get(position); } @Override public void onNothingSelected(AdapterView<?> parent) {}} ); spinnerArea.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // country = provence.getCityList().get(position).getCountryList().get(position); } @Override public void onNothingSelected(AdapterView<?> parent) {} }); /*-------------------------------------------------省市区三联动 end------------------------------------------------------*/
4、然后选取spinner相应index对应省市区的编号返回服务器,大功告成。
一些需要初始化的东东
/* 省市区实体类*/ private ProvinceArea provence; private CityArea city; private CountryArea country; /*省市区适配器*/ private ArrayAdapter<ProvinceArea> adapter01; private ArrayAdapter<CityArea> adapter02; private ArrayAdapter<CountryArea> adapter03; /*省市区集合*/ private ArrayList<ProvinceArea> pData = new ArrayList<ProvinceArea>(); private ArrayList<CityArea> cityData; private ArrayList<CountryArea> countryData; private ArrayList<String> provinceCode = new ArrayList<String>(); private ArrayList<String> provinceName = new ArrayList<String>();
ps:一般情况下,spinner默认选中index为0的项,这时候在获取城市编号的时候,会报空指针;需要加如上代码
provence = pData.get(0); <pre name="code" class="html">city = pData.get(0).getCityList().get(0); ...
如有不当之处,敬请指出。