android在代码中利用Spinner控件设置联动地区的解决办法

需求就是地区选择可以弹出来,因为百度地图一般是获取当前的地址,我们需要的是他的家庭地址  所以才有了三级Spinner解决


地区问题  就是当用户填写了之后,下次再修改,你要给他显示出来上次填写的值,由于是联动的比较麻烦


首先是要搞定地区的问题,一般是用array来设置  这里面我只贴一部分

    
    
        不限地区
        北京
        天津
        河北
        山西
        内蒙古
        辽宁
        吉林
        黑龙江
        上海
        江苏
        浙江
        安徽
        福建
        江西
        山东
        河南
        湖北
        湖南
        广东
        广西
        海南
        重庆
        四川
        贵州
        云南
        西藏
        陕西
        甘肃
        青海
        宁夏
        新疆
    

    
    
        不限地区
    
    
        北京
    
    
        天津
    
    
        石家庄
        唐山
        秦皇岛
        邯郸
        邢台
        保定
        张家口
        承德

然后把这个东西搞成一个类来装

public class ResourceData {
	
	public static final int[] city = { R.array.beijin_province_item, R.array.tianjin_province_item, R.array.heibei_province_item,
			R.array.shanxi1_province_item, R.array.neimenggu_province_item, R.array.liaoning_province_item,
			R.array.jilin_province_item, R.array.heilongjiang_province_item, R.array.shanghai_province_item,
			R.array.jiangsu_province_item, R.array.zhejiang_province_item, R.array.anhui_province_item,
			R.array.fujian_province_item, R.array.jiangxi_province_item, R.array.shandong_province_item,
			R.array.henan_province_item, R.array.hubei_province_item, R.array.hunan_province_item,
			R.array.guangdong_province_item, R.array.guangxi_province_item, R.array.hainan_province_item,
			R.array.chongqing_province_item, R.array.sichuan_province_item, R.array.guizhou_province_item,
			R.array.yunnan_province_item, R.array.xizang_province_item, R.array.shanxi2_province_item,
			R.array.gansu_province_item, R.array.qinghai_province_item, R.array.linxia_province_item,
			R.array.xinjiang_province_item };
}

这里面只写一部分了  多个地级市,县城你自己加吧


接下来就是代码中搞定了:


要准备一个函数:

	private void select(Spinner spin, ArrayAdapter adapter, int arry,int position) {
		adapter = ArrayAdapter.createFromResource(context, arry, R.drawable.drop_list_hover);
		adapter.setDropDownViewResource(R.drawable.drop_list_ys);
		spin.setAdapter(adapter);
		spin.setSelection(position, true);
	}

这个函数的功能就是给一个 Spinner进行初始化把,参数分别是Spinner对象,适配器,数据集,默认位置


因为在设置联动的时候都是根据上一个Spinner选择的值 id来决定下一个Spinner的值


接下来就先贴代码了  


	private void loadAddsSpinner() {
		province_spinner = (Spinner) findViewById(R.id.province_spinner);
		province_spinner.setPrompt("请选择省份");
		province_adapter = ArrayAdapter.createFromResource(context, R.array.province_item,
				R.drawable.drop_list_hover);
		province_adapter.setDropDownViewResource(R.drawable.drop_list_ys);
		province_spinner.setAdapter(province_adapter);
		province_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
				provinceId = province_spinner.getSelectedItemPosition();
				strProvince = province_spinner.getSelectedItem().toString();
				city_spinner = (Spinner) findViewById(R.id.city_spinner);
				if (true) {
					Log.v("test", "province: " + province_spinner.getSelectedItem().toString() + provinceId.toString());
					county_spinner = (Spinner) findViewById(R.id.county_spinner);
					city_spinner = (Spinner) findViewById(R.id.city_spinner);
					city_spinner.setPrompt("请选择城市");
					select(city_spinner, city_adapter, ResourceData.city[provinceId],sposition);
					city_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

						@Override
						public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
							cityId = city_spinner.getSelectedItemPosition();
							strCity = city_spinner.getSelectedItem().toString();
							Log.v("test", "city: " + city_spinner.getSelectedItem().toString() + cityId.toString());
							if (true) {
								county_spinner = (Spinner) findViewById(R.id.county_spinner);
								county_spinner.setPrompt("请选择县区");
								switch (provinceId) {
								case 0:
									select(county_spinner, county_adapter, ResourceData.countyOfBeiJing[cityId],xianposition);
									break;
								case 1:
									select(county_spinner, county_adapter, ResourceData.countyOfTianJing[cityId],xianposition);
									break;
								case 2:
									select(county_spinner, county_adapter, ResourceData.countyOfHeBei[cityId],xianposition);
									break;
								case 3:
									select(county_spinner, county_adapter, ResourceData.countyOfShanXi1[cityId],xianposition);
									break;
								case 4:
									select(county_spinner, county_adapter, ResourceData.countyOfNeiMengGu[cityId],xianposition);
									break;
								case 5:
									select(county_spinner, county_adapter, ResourceData.countyOfLiaoNing[cityId],xianposition);
									break;
								case 6:
									select(county_spinner, county_adapter, ResourceData.countyOfJiLin[cityId],xianposition);
									break;
								case 7:
									select(county_spinner, county_adapter, ResourceData.countyOfHeiLongJiang[cityId],xianposition);
									break;
								case 8:
									select(county_spinner, county_adapter, ResourceData.countyOfShangHai[cityId],xianposition);
									break;
								case 9:
									select(county_spinner, county_adapter, ResourceData.countyOfJiangSu[cityId],xianposition);
									break;
								case 10:
									select(county_spinner, county_adapter, ResourceData.countyOfZheJiang[cityId],xianposition);
									break;
								case 11:
									select(county_spinner, county_adapter, ResourceData.countyOfAnHui[cityId],xianposition);
									break;
								case 12:
									select(county_spinner, county_adapter, ResourceData.countyOfFuJian[cityId],xianposition);
									break;
								case 13:
									select(county_spinner, county_adapter, ResourceData.countyOfJiangXi[cityId],xianposition);
									break;
								case 14:
									select(county_spinner, county_adapter, ResourceData.countyOfShanDong[cityId],xianposition);
									break;
								case 15:
									select(county_spinner, county_adapter, ResourceData.countyOfHeNan[cityId],xianposition);
									break;
								case 16:
									select(county_spinner, county_adapter, ResourceData.countyOfHuBei[cityId],xianposition);
									break;
								case 17:
									select(county_spinner, county_adapter, ResourceData.countyOfHuNan[cityId],xianposition);
									break;
								case 18:
									select(county_spinner, county_adapter, ResourceData.countyOfGuangDong[cityId],xianposition);
									break;
								case 19:
									select(county_spinner, county_adapter, ResourceData.countyOfGuangXi[cityId],xianposition);
									break;
								case 20:
									select(county_spinner, county_adapter, ResourceData.countyOfHaiNan[cityId],xianposition);
									break;
								case 21:
									select(county_spinner, county_adapter, ResourceData.countyOfChongQing[cityId],xianposition);
									break;
								case 22:
									select(county_spinner, county_adapter, ResourceData.countyOfSiChuan[cityId],xianposition);
									break;
								case 23:
									select(county_spinner, county_adapter, ResourceData.countyOfGuiZhou[cityId],xianposition);
									break;
								case 24:
									select(county_spinner, county_adapter, ResourceData.countyOfYunNan[cityId],xianposition);
									break;
								case 25:
									select(county_spinner, county_adapter, ResourceData.countyOfXiZang[cityId],xianposition);
									break;
								case 26:
									select(county_spinner, county_adapter, ResourceData.countyOfShanXi2[cityId],xianposition);
									break;
								case 27:
									select(county_spinner, county_adapter, ResourceData.countyOfGanSu[cityId],xianposition);
									break;
								case 28:
									select(county_spinner, county_adapter, ResourceData.countyOfQingHai[cityId],xianposition);
									break;
								case 29:
									select(county_spinner, county_adapter, ResourceData.countyOfNingXia[cityId],xianposition);
									break;
								case 30:
									select(county_spinner, county_adapter, ResourceData.countyOfXinJiang[cityId],xianposition);

								default:
									break;
								}

								county_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

									@Override
									public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
										strCounty = county_spinner.getSelectedItem().toString();
										if (strProvince.equals(strCity)) {
											xiangxiadd.setText(strProvince + strCounty);
											region = strProvince + "," + strCounty;
											xiangxiaddress = region +","+xiangxiaddress;//详细地址加上前缀
										} else {
											xiangxiadd.setText(strProvince + strCity + strCounty);
											region = strProvince + "," + strCity + "," + strCounty;
											xiangxiaddress = region +","+xiangxiaddress;//详细地址加上前缀
										}
									}

									@Override
									public void onNothingSelected(AdapterView arg0) {

									}

								});
							}
						}

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

						}

					});
				}
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {

			}
		});

	}


这里面最重要的就是select代码了  注意最后一个参数的作用  position  他就是用来设置默认值的


后面就简单了,分析从服务器中返回的数据,判断是哪个id   变成position就好了


我的是这样做的,可能还有人更坚定,我感觉我的稍显负责


							//省市县
							String ssxStr = element.optString("ind_origin2");
							if(ssxStr!=null || ssxStr !="" || ssxStr!="null"){
								String[] ss = ssxStr.split(",");
								Log.i("Wusheng", ss.length+"-----322125");
								if(ss.length == 2){
									province_spinner.setSelection(initProSpinner(ss[0]), true);
									int proInt = initProSpinner(ss[0]);
									int cityInt = initCitySpinner(proInt,ss[0]);
									sposition = cityInt;
									provinceId = proInt;
									cityId = cityInt;
									int xianInt = initCountSpinner(proInt,sposition,ss[1]);
									xianposition = xianInt;
									//Log.i("Wusheng",ResourceData.city[initProSpinner(ss[0])]);
								}else if(ss.length == 3){
									Log.i("Wusheng", initProSpinner(ss[0])+"322125");
									province_spinner.setSelection(initProSpinner(ss[0]), true);
									int proInt = initProSpinner(ss[0]);
									int cityInt = initCitySpinner(proInt,ss[1]);
									sposition = cityInt;
									provinceId = proInt;
									cityId = cityInt;
									//接下来通过省,市的代码去找到县的代码
									int xianInt = initCountSpinner(proInt,sposition,ss[2]);
									Log.i("Wusheng",xianInt+"-----44---322125");
									xianposition = xianInt;
								}							
							}

这里面是异步进程handler里面的处理省 市县的代码  为什么切分之后分为两种呢,因为我们有北京  重庆这样的特殊省份


根据省的名称返回省的代码:

	/**
	 * 根据省名称返回  下标值
	 * @param proStr
	 * @return
	 */
	public int initProSpinner(String proStr){
		String[] pros=this.getResources().getStringArray(R.array.province_item);
		for(int i=0;i

看客不要太在意我定义了那么多int变量啊,因为怕写错了,所以分的比较清楚,后面的很多int都去掉了,

	/**
	 * 根据省代码 来获取 市的代码
	 * @param proInt
	 * @param string
	 * @return
	 */
	public int initCitySpinner(int proInt, String citStr) {
		// TODO Auto-generated method stub
		String[] citys=this.getResources().getStringArray(ResourceData.city[proInt]);
		for(int i=0;i

根据省代码去获取 市的代码,你可能会认为,为什么上面不根据名称直接去获取市的代码,那你想多了,你可以看看我的根据array构造数组的时候怎么调用id值的


	/**
	 * 根据省 市代码 来获取 县的代码
	 * @param sposition2
	 * @param string
	 * @return
	 */
	public int initCountSpinner(int proint,int sposition2, String countStr) {
		// TODO Auto-generated method stub
		String[] citys= null;
		switch (proint) {
		case 0:
			citys = this.getResources().getStringArray(ResourceData.countyOfBeiJing[sposition2]);
			break;
		case 1:
			citys = this.getResources().getStringArray(ResourceData.countyOfTianJing[sposition2]);
			break;
		case 2:
			citys = this.getResources().getStringArray(ResourceData.countyOfHeBei[sposition2]);
			break;
		case 3:
			citys = this.getResources().getStringArray(ResourceData.countyOfShanXi1[sposition2]);
			break;
		case 4:
			citys = this.getResources().getStringArray(ResourceData.countyOfNeiMengGu[sposition2]);
			break;
		case 5:
			citys = this.getResources().getStringArray(ResourceData.countyOfLiaoNing[sposition2]);
			break;
		case 6:
			citys = this.getResources().getStringArray(ResourceData.countyOfJiLin[sposition2]);
			break;
		case 7:
			citys = this.getResources().getStringArray(ResourceData.countyOfHeiLongJiang[sposition2]);
			break;
		case 8:
			citys = this.getResources().getStringArray(ResourceData.countyOfShangHai[sposition2]);
			break;
		case 9:
			citys = this.getResources().getStringArray(ResourceData.countyOfJiangSu[sposition2]);
			break;
		case 10:
			citys = this.getResources().getStringArray(ResourceData.countyOfZheJiang[sposition2]);
			break;
		case 11:
			citys = this.getResources().getStringArray(ResourceData.countyOfAnHui[sposition2]);
			break;
		case 12:
			citys = this.getResources().getStringArray(ResourceData.countyOfFuJian[sposition2]);
			break;
		case 13:
			citys = this.getResources().getStringArray(ResourceData.countyOfJiangXi[sposition2]);
			break;
		case 14:
			citys = this.getResources().getStringArray(ResourceData.countyOfShanDong[sposition2]);
			break;
		case 15:
			citys = this.getResources().getStringArray(ResourceData.countyOfHeNan[sposition2]);
			break;
		case 16:
			citys = this.getResources().getStringArray(ResourceData.countyOfHuBei[sposition2]);
			break;
		case 17:
			citys = this.getResources().getStringArray(ResourceData.countyOfHuNan[sposition2]);
			break;
		case 18:
			citys = this.getResources().getStringArray(ResourceData.countyOfGuangDong[sposition2]);
			break;
		case 19:
			citys = this.getResources().getStringArray(ResourceData.countyOfGuangXi[sposition2]);
			break;
		case 20:
			citys = this.getResources().getStringArray(ResourceData.countyOfHaiNan[sposition2]);
			break;
		case 21:
			citys = this.getResources().getStringArray(ResourceData.countyOfChongQing[sposition2]);
			break;
		case 22:
			citys = this.getResources().getStringArray(ResourceData.countyOfSiChuan[sposition2]);
			break;
		case 23:
			citys = this.getResources().getStringArray(ResourceData.countyOfGuiZhou[sposition2]);
			break;
		case 24:
			citys = this.getResources().getStringArray(ResourceData.countyOfYunNan[sposition2]);
			break;
		case 25:
			citys = this.getResources().getStringArray(ResourceData.countyOfXiZang[sposition2]);
			break;
		case 26:
			citys = this.getResources().getStringArray(ResourceData.countyOfShanXi2[sposition2]);
			break;
		case 27:
			citys = this.getResources().getStringArray(ResourceData.countyOfGanSu[sposition2]);
			break;
		case 28:
			citys = this.getResources().getStringArray(ResourceData.countyOfQingHai[sposition2]);
			break;
		case 29:
			citys = this.getResources().getStringArray(ResourceData.countyOfNingXia[sposition2]);
			break;
		case 30:
			citys = this.getResources().getStringArray(ResourceData.countyOfXinJiang[sposition2]);

		default:
			break;
		}
		for(int i=0;i

这个地方就稍微县的臃肿了,那位看客又更好的方法没有,中国低级县区太多了,至少有2800个左右吧,我也想不出来怎么用代码更好的找到每个县的代码


到此基本就完成了认为,运行下就解决了问题


其中解析那里用的是  当前类的私有变量   不用多说了

你可能感兴趣的:(各种BUG,Android学习)