前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android在代码中利用Spinner控件设置联动地区的解决办法

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

作者头像
wust小吴
发布2019-07-08 16:16:32
2.1K0
发布2019-07-08 16:16:32
举报
文章被收录于专栏:风吹杨柳

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

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

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

代码语言:javascript
复制
    <!-- 含个省、自治区、直辖市 -->
    <string-array name="province_item">
        <item>不限地区</item>
        <item>北京</item>
        <item>天津</item>
        <item>河北</item>
        <item>山西</item>
        <item>内蒙古</item>
        <item>辽宁</item>
        <item>吉林</item>
        <item>黑龙江</item>
        <item>上海</item>
        <item>江苏</item>
        <item>浙江</item>
        <item>安徽</item>
        <item>福建</item>
        <item>江西</item>
        <item>山东</item>
        <item>河南</item>
        <item>湖北</item>
        <item>湖南</item>
        <item>广东</item>
        <item>广西</item>
        <item>海南</item>
        <item>重庆</item>
        <item>四川</item>
        <item>贵州</item>
        <item>云南</item>
        <item>西藏</item>
        <item>陕西</item>
        <item>甘肃</item>
        <item>青海</item>
        <item>宁夏</item>
        <item>新疆</item>
    </string-array>

    <!-- 个地级市 -->
    <string-array name="unlimited_province_item">
        <item>不限地区</item>
    </string-array>
    <string-array name="beijin_province_item">
        <item>北京</item>
    </string-array>
    <string-array name="tianjin_province_item">
        <item>天津</item>
    </string-array>
    <string-array name="heibei_province_item">
        <item>石家庄</item>
        <item>唐山</item>
        <item>秦皇岛</item>
        <item>邯郸</item>
        <item>邢台</item>
        <item>保定</item>
        <item>张家口</item>
        <item>承德</item>

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

代码语言:javascript
复制
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 };
}

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

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

要准备一个函数:

代码语言:javascript
复制
	private void select(Spinner spin, ArrayAdapter<CharSequence> 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的值

接下来就先贴代码了  

代码语言:javascript
复制
	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就好了

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

代码语言:javascript
复制
							//省市县
							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里面的处理省 市县的代码  为什么切分之后分为两种呢,因为我们有北京  重庆这样的特殊省份

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

代码语言:javascript
复制
	/**
	 * 根据省名称返回  下标值
	 * @param proStr
	 * @return
	 */
	public int initProSpinner(String proStr){
		String[] pros=this.getResources().getStringArray(R.array.province_item);
		for(int i=0;i<pros.length;i++){
			if(proStr.equals(pros[i])){
				return i;
			}
		}
		return 0;
	}

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

代码语言:javascript
复制
	/**
	 * 根据省代码 来获取 市的代码
	 * @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<citys.length;i++){
			if(citStr.equals(citys[i])){
				return i;
			}
		}
		return 0;
	}

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

代码语言:javascript
复制
	/**
	 * 根据省 市代码 来获取 县的代码
	 * @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<citys.length;i++){
			if(countStr.equals(citys[i])){
				return i;
			}
		}
		return 0;
	}

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

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年10月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档