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

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

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

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

    <!-- 含个省、自治区、直辖市 -->
    <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>

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

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<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的值

接下来就先贴代码了  

	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<pros.length;i++){
			if(proStr.equals(pros[i])){
				return i;
			}
		}
		return 0;
	}

看客不要太在意我定义了那么多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<citys.length;i++){
			if(citStr.equals(citys[i])){
				return i;
			}
		}
		return 0;
	}

根据省代码去获取 市的代码,你可能会认为,为什么上面不根据名称直接去获取市的代码,那你想多了,你可以看看我的根据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<citys.length;i++){
			if(countStr.equals(citys[i])){
				return i;
			}
		}
		return 0;
	}

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

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Spark学习技巧

Java并发锁(一):悲观锁与乐观锁

今天我们来聊下线程中的悲观锁和乐观锁,首先提到"悲观锁","乐观锁"提到这两个名词,大家可能会先想到数据库。注意啦,我们这里讲的是多线程中的锁,而不是数据库中的...

31320
来自专栏Spark学习技巧

Spark Core读取ES的分区问题分析

ES也是比较火热,在日志数据分析,规则分析等确实很方便,说实话用es stack 浪尖觉得可以解决很多公司的数据分析需求。极客时间下周一要上线新的ES课程,有需...

21340
来自专栏Bingo的深度学习杂货店

Leetcode 【216、769】

这道题一看要求输出所有满足题意的组合,很明显 DFS 回溯法进行求解,属于模板题。不过这倒题需要注意几个剪枝情况:

7710
来自专栏挖数

Python人脸合成,秒变胡歌王俊凯

如今,随着技术的不断进步,“变脸”技术不再是四川喜剧的“独门武功”。运用机器学习的方法,我们同样可以实现人脸“融合”。当然这里说的人脸融合指的是将两个人的人脸照...

26410
来自专栏IT技术精选文摘

面试题: 了解OO的SOLID原则吗

一个类只应承担一种责任。换句话说,让一个类只做一件事。如果需要承担更多的工作,那么分解这个类。

13620
来自专栏Java研发军团

数据结构+算法(第13篇):精通二叉树的“独门忍术”——线索二叉树(上)

如果要写出非递归的遍历算法,无论用哪种遍历方法,根据《再不会“降维打击”你就Out了!》《神力加身!动态编程》《史上最猛之递归屠龙奥义》三篇文章中讲到的知识和技...

36520
来自专栏机器学习与python集中营

python代码调试神器PySnooper

| 导语 最近在github上冒出了一个python的debug神器PySnooper,号称在debug时可以消灭print。那么该工具有哪些优点呢,如何使用该...

24420
来自专栏Android知识分享

Android自定义View绘制前的准备:DecorView创建 & 显示

回忆前文:Android自定义View基础:ViewRoot、DecorView & Window的简介,可看出最后1步 = 绘制

14720
来自专栏Jerry的SAP技术分享

VM stack in Chrome

http://stackoverflow.com/questions/17367560/chrome-development-tool-vm-file-from...

13010
来自专栏业余草

常见算法的时间复杂度 Ο(1)<Ο(log2n)<Ο(n)<Ο(nlog2n)<Ο(n2)<Ο(n3)<…

说实话,我是真的不懂算法。但是,我知道一个算法的好坏,通常时间复杂度是一个评价的指标之一。

28020

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励