关闭

Android UI控件之Spinner实现省市区三级联动

标签: spinner控件ui三级联动
728人阅读 评论(0) 收藏 举报

城市信息我将他放在strings.xml文件中,具体就不给出了,信息太大了

主xml文件如下:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:focusableInTouchMode="false" >
  6. <TableLayout
  7. android:id="@+id/tableLayout1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:paddingTop="20px"
  11. android:stretchColumns="1" >
  12. <TableRow
  13. android:id="@+id/tableRow1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:paddingTop="10px"
  17. android:scrollbarAlwaysDrawVerticalTrack="false" >
  18. <TextView
  19. android:id="@+id/province_text"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="省份:" >
  23. </TextView>
  24. <Spinner
  25. android:id="@+id/province_spinner"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content" >
  28. </Spinner>
  29. </TableRow>
  30. <TableRow
  31. android:id="@+id/tableRow2"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:paddingTop="10px"
  35. android:scrollbarAlwaysDrawVerticalTrack="false" >
  36. <TextView
  37. android:id="@+id/city_text"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="城市:" >
  41. </TextView>
  42. <Spinner
  43. android:id="@+id/city_spinner"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content" >
  46. </Spinner>
  47. </TableRow>
  48. <TableRow
  49. android:id="@+id/tableRow3"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:paddingTop="10px"
  53. android:scrollbarAlwaysDrawVerticalTrack="false" >
  54. <TextView
  55. android:id="@+id/county_text"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="县城镇:" >
  59. </TextView>
  60. <Spinner
  61. android:id="@+id/county_spinner"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content" >
  64. </Spinner>
  65. </TableRow>
  66. <EditText
  67. android:id="@+id/display_edit"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:focusableInTouchMode="false"
  71. android:hint="输出结果"
  72. android:paddingTop="10px" >
  73. </EditText>
  74. </TableLayout>
  75. </RelativeLayout>
[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:focusableInTouchMode="false" >  
  6.   
  7.     <TableLayout  
  8.         android:id="@+id/tableLayout1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:paddingTop="20px"  
  12.         android:stretchColumns="1" >  
  13.   
  14.         <TableRow  
  15.             android:id="@+id/tableRow1"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:paddingTop="10px"  
  19.             android:scrollbarAlwaysDrawVerticalTrack="false" >  
  20.   
  21.             <TextView  
  22.                 android:id="@+id/province_text"  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:text="省份:" >  
  26.             </TextView>  
  27.   
  28.             <Spinner  
  29.                 android:id="@+id/province_spinner"  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content" >  
  32.             </Spinner>  
  33.         </TableRow>  
  34.   
  35.         <TableRow  
  36.             android:id="@+id/tableRow2"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:paddingTop="10px"  
  40.             android:scrollbarAlwaysDrawVerticalTrack="false" >  
  41.   
  42.             <TextView  
  43.                 android:id="@+id/city_text"  
  44.                 android:layout_width="wrap_content"  
  45.                 android:layout_height="wrap_content"  
  46.                 android:text="城市:" >  
  47.             </TextView>  
  48.   
  49.             <Spinner  
  50.                 android:id="@+id/city_spinner"  
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content" >  
  53.             </Spinner>  
  54.         </TableRow>  
  55.   
  56.         <TableRow  
  57.             android:id="@+id/tableRow3"  
  58.             android:layout_width="wrap_content"  
  59.             android:layout_height="wrap_content"  
  60.             android:paddingTop="10px"  
  61.             android:scrollbarAlwaysDrawVerticalTrack="false" >  
  62.   
  63.             <TextView  
  64.                 android:id="@+id/county_text"  
  65.                 android:layout_width="wrap_content"  
  66.                 android:layout_height="wrap_content"  
  67.                 android:text="县城镇:" >  
  68.             </TextView>  
  69.   
  70.             <Spinner  
  71.                 android:id="@+id/county_spinner"  
  72.                 android:layout_width="wrap_content"  
  73.                 android:layout_height="wrap_content" >  
  74.             </Spinner>  
  75.         </TableRow>  
  76.   
  77.         <EditText  
  78.             android:id="@+id/display_edit"  
  79.             android:layout_width="wrap_content"  
  80.             android:layout_height="wrap_content"  
  81.             android:focusableInTouchMode="false"  
  82.             android:hint="输出结果"  
  83.             android:paddingTop="10px" >  
  84.         </EditText>  
  85.     </TableLayout>  
  86.   
  87. </RelativeLayout>  
MainAcitivty问价如下:

  1. /**
  2. * 省市县联动
  3. *
  4. * kiritor
  5. *
  6. * 2011.3.17
  7. */
  8. package com.kiritor.ui_spinner_baseadapter;
  9. import android.app.Activity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.AdapterView;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.EditText;
  16. import android.widget.Spinner;
  17. import android.widget.AdapterView.OnItemSelectedListener;
  18. public class MainActivityextends Activity {
  19. private Spinner province_spinner;
  20. private Spinner city_spinner;
  21. private Spinner county_spinner;
  22. private Integer provinceId, cityId;
  23. private EditText display;
  24. private String strProvince, strCity, strCounty;
  25. //市,自治区集合
  26. private int[] city = { R.array.beijin_province_item,
  27. R.array.tianjin_province_item, R.array.heibei_province_item,
  28. R.array.shanxi1_province_item, R.array.neimenggu_province_item,
  29. R.array.liaoning_province_item, R.array.jilin_province_item,
  30. R.array.heilongjiang_province_item, R.array.shanghai_province_item,
  31. R.array.jiangsu_province_item, R.array.zhejiang_province_item,
  32. R.array.anhui_province_item, R.array.fujian_province_item,
  33. R.array.jiangxi_province_item, R.array.shandong_province_item,
  34. R.array.henan_province_item, R.array.hubei_province_item,
  35. R.array.hunan_province_item, R.array.guangdong_province_item,
  36. R.array.guangxi_province_item, R.array.hainan_province_item,
  37. R.array.chongqing_province_item, R.array.sichuan_province_item,
  38. R.array.guizhou_province_item, R.array.yunnan_province_item,
  39. R.array.xizang_province_item, R.array.shanxi2_province_item,
  40. R.array.gansu_province_item, R.array.qinghai_province_item,
  41. R.array.linxia_province_item, R.array.xinjiang_province_item,
  42. R.array.hongkong_province_item, R.array.aomen_province_item,
  43. R.array.taiwan_province_item };
  44. private int[] countyOfBeiJing = { R.array.beijin_city_item };
  45. private int[] countyOfTianJing = { R.array.tianjin_city_item };
  46. private int[] countyOfHeBei = { R.array.shijiazhuang_city_item,
  47. R.array.tangshan_city_item, R.array.qinghuangdao_city_item,
  48. R.array.handan_city_item, R.array.xingtai_city_item,
  49. R.array.baoding_city_item, R.array.zhangjiakou_city_item,
  50. R.array.chengde_city_item, R.array.cangzhou_city_item,
  51. R.array.langfang_city_item, R.array.hengshui_city_item };
  52. private int[] countyOfShanXi1 = { R.array.taiyuan_city_item,
  53. R.array.datong_city_item, R.array.yangquan_city_item,
  54. R.array.changzhi_city_item, R.array.jincheng_city_item,
  55. R.array.shuozhou_city_item, R.array.jinzhong_city_item,
  56. R.array.yuncheng_city_item, R.array.xinzhou_city_item,
  57. R.array.linfen_city_item, R.array.lvliang_city_item };
  58. private int[] countyOfNeiMengGu = { R.array.huhehaote_city_item,
  59. R.array.baotou_city_item, R.array.wuhai_city_item,
  60. R.array.chifeng_city_item, R.array.tongliao_city_item,
  61. R.array.eerduosi_city_item, R.array.hulunbeier_city_item,
  62. R.array.bayannaoer_city_item, R.array.wulanchabu_city_item,
  63. R.array.xinganmeng_city_item, R.array.xilinguolemeng_city_item,
  64. R.array.alashanmeng_city_item };
  65. private int[] countyOfLiaoNing = { R.array.shenyang_city_item,
  66. R.array.dalian_city_item, R.array.anshan_city_item,
  67. R.array.wushun_city_item, R.array.benxi_city_item,
  68. R.array.dandong_city_item, R.array.liaoning_jinzhou_city_item,
  69. R.array.yingkou_city_item, R.array.fuxin_city_item,
  70. R.array.liaoyang_city_item, R.array.panjin_city_item,
  71. R.array.tieling_city_item, R.array.zhaoyang_city_item,
  72. R.array.huludao_city_item };
  73. private int[] countyOfJiLin = { R.array.changchun_city_item,
  74. R.array.jilin_city_item, R.array.siping_city_item,
  75. R.array.liaoyuan_city_item, R.array.tonghua_city_item,
  76. R.array.baishan_city_item, R.array.songyuan_city_item,
  77. R.array.baicheng_city_item, R.array.yanbian_city_item };
  78. private int[] countyOfHeiLongJiang = { R.array.haerbing_city_item,
  79. R.array.qiqihaer_city_item, R.array.jixi_city_item,
  80. R.array.hegang_city_item, R.array.shuangyashan_city_item,
  81. R.array.daqing_city_item, R.array.heilongjiang_yichun_city_item,
  82. R.array.jiamusi_city_item, R.array.qitaihe_city_item,
  83. R.array.mudanjiang_city_item, R.array.heihe_city_item,
  84. R.array.suihua_city_item, R.array.daxinganling_city_item };
  85. private int[] countyOfShangHai = { R.array.shanghai_city_item };
  86. private int[] countyOfJiangSu = { R.array.nanjing_city_item,
  87. R.array.wuxi_city_item, R.array.xuzhou_city_item,
  88. R.array.changzhou_city_item, R.array.nanjing_suzhou_city_item,
  89. R.array.nantong_city_item, R.array.lianyungang_city_item,
  90. R.array.huaian_city_item, R.array.yancheng_city_item,
  91. R.array.yangzhou_city_item, R.array.zhenjiang_city_item,
  92. R.array.jiangsu_taizhou_city_item, R.array.suqian_city_item };
  93. private int[] countyOfZheJiang = { R.array.hangzhou_city_item,
  94. R.array.ningbo_city_item, R.array.wenzhou_city_item,
  95. R.array.jiaxing_city_item, R.array.huzhou_city_item,
  96. R.array.shaoxing_city_item, R.array.jinhua_city_item,
  97. R.array.quzhou_city_item, R.array.zhoushan_city_item,
  98. R.array.zejiang_huzhou_city_item, R.array.lishui_city_item };
  99. private int[] countyOfAnHui = { R.array.hefei_city_item,
  100. R.array.wuhu_city_item, R.array.bengbu_city_item,
  101. R.array.huainan_city_item, R.array.maanshan_city_item,
  102. R.array.huaibei_city_item, R.array.tongling_city_item,
  103. R.array.anqing_city_item, R.array.huangshan_city_item,
  104. R.array.chuzhou_city_item, R.array.fuyang_city_item,
  105. R.array.anhui_suzhou_city_item, R.array.chaohu_city_item,
  106. R.array.luan_city_item, R.array.haozhou_city_item,
  107. R.array.chizhou_city_item, R.array.xuancheng_city_item };
  108. private int[] countyOfFuJian = { R.array.huzhou_city_item,
  109. R.array.xiamen_city_item, R.array.putian_city_item,
  110. R.array.sanming_city_item, R.array.quanzhou_city_item,
  111. R.array.zhangzhou_city_item, R.array.nanp_city_item,
  112. R.array.longyan_city_item, R.array.ningde_city_item };
  113. private int[] countyOfJiangXi = { R.array.nanchang_city_item,
  114. R.array.jingdezhen_city_item, R.array.pingxiang_city_item,
  115. R.array.jiujiang_city_item, R.array.xinyu_city_item,
  116. R.array.yingtan_city_item, R.array.ganzhou_city_item,
  117. R.array.jian_city_item, R.array.jiangxi_yichun_city_item,
  118. R.array.jiangxi_wuzhou_city_item, R.array.shangrao_city_item };
  119. private int[] countyOfShanDong = { R.array.jinan_city_item,
  120. R.array.qingdao_city_item, R.array.zaobo_city_item,
  121. R.array.zaozhuang_city_item, R.array.dongying_city_item,
  122. R.array.yantai_city_item, R.array.weifang_city_item,
  123. R.array.jining_city_item, R.array.taian_city_item,
  124. R.array.weihai_city_item, R.array.rizhao_city_item,
  125. R.array.laiwu_city_item, R.array.linxi_city_item,
  126. R.array.dezhou_city_item, R.array.liaocheng_city_item,
  127. R.array.shandong_bingzhou_city_item, R.array.heze_city_item };
  128. private int[] countyOfHeNan = { R.array.zhenshou_city_item,
  129. R.array.kaifang_city_item, R.array.luoyang_city_item,
  130. R.array.kaipingshan_city_item, R.array.anyang_city_item,
  131. R.array.hebi_city_item, R.array.xinxiang_city_item,
  132. R.array.jiaozuo_city_item, R.array.buyang_city_item,
  133. R.array.xuchang_city_item, R.array.leihe_city_item,
  134. R.array.sanmenxia_city_item, R.array.nanyang_city_item,
  135. R.array.shangqiu_city_item, R.array.xinyang_city_item,
  136. R.array.zhoukou_city_item, R.array.zhumadian_city_item };
  137. private int[] countyOfHuBei = { R.array.wuhan_city_item,
  138. R.array.huangshi_city_item, R.array.shiyan_city_item,
  139. R.array.yichang_city_item, R.array.xiangpan_city_item,
  140. R.array.erzhou_city_item, R.array.jinmen_city_item,
  141. R.array.xiaogan_city_item, R.array.hubei_jinzhou_city_item,
  142. R.array.huanggang_city_item, R.array.xianning_city_item,
  143. R.array.suizhou_city_item, R.array.enshi_city_item,
  144. R.array.shenglongjia_city_item };
  145. private int[] countyOfHuNan = { R.array.changsha_city_item,
  146. R.array.zhuzhou_city_item, R.array.xiangtan_city_item,
  147. R.array.hengyang_city_item, R.array.shaoyang_city_item,
  148. R.array.yueyang_city_item, R.array.changde_city_item,
  149. R.array.zhangjiajie_city_item, R.array.yiyang_city_item,
  150. R.array.hunan_bingzhou_city_item, R.array.yongzhou_city_item,
  151. R.array.huaihua_city_item, R.array.loudi_city_item,
  152. R.array.xiangxi_city_item };
  153. private int[] countyOfGuangDong = { R.array.guangzhou_city_item,
  154. R.array.shaoguan_city_item, R.array.shenzhen_city_item,
  155. R.array.zhuhai_city_item, R.array.shantou_city_item,
  156. R.array.foshan_city_item, R.array.jiangmen_city_item,
  157. R.array.zhangjiang_city_item, R.array.maoming_city_item,
  158. R.array.zhaoqing_city_item, R.array.huizhou_city_item,
  159. R.array.meizhou_city_item, R.array.shanwei_city_item,
  160. R.array.heyuan_city_item, R.array.yangjiang_city_item,
  161. R.array.qingyuan_city_item, R.array.dongguan_city_item,
  162. R.array.zhongshan_city_item, R.array.chaozhou_city_item,
  163. R.array.jiyang_city_item, R.array.yunfu_city_item };
  164. private int[] countyOfGuangXi = { R.array.nanning_city_item,
  165. R.array.liuzhou_city_item, R.array.guilin_city_item,
  166. R.array.guangxi_wuzhou_city_item, R.array.beihai_city_item,
  167. R.array.fangchenggang_city_item, R.array.qinzhou_city_item,
  168. R.array.guigang_city_item, R.array.yuelin_city_item,
  169. R.array.baise_city_item, R.array.hezhou_city_item,
  170. R.array.hechi_city_item, R.array.laibing_city_item,
  171. R.array.chuangzuo_city_item };
  172. private int[] countyOfHaiNan = { R.array.haikou_city_item,
  173. R.array.sanya_city_item };
  174. private int[] countyOfChongQing = { R.array.chongqing_city_item };
  175. private int[] countyOfSiChuan = { R.array.chengdu_city_item,
  176. R.array.zigong_city_item, R.array.panzhihua_city_item,
  177. R.array.luzhou_city_item, R.array.deyang_city_item,
  178. R.array.mianyang_city_item, R.array.guangyuan_city_item,
  179. R.array.suining_city_item, R.array.neijiang_city_item,
  180. R.array.leshan_city_item, R.array.nanchong_city_item,
  181. R.array.meishan_city_item, R.array.yibing_city_item,
  182. R.array.guangan_city_item, R.array.dazhou_city_item,
  183. R.array.yaan_city_item, R.array.bazhong_city_item,
  184. R.array.ziyang_city_item, R.array.abei_city_item,
  185. R.array.ganmu_city_item, R.array.liangshan_city_item };
  186. private int[] countyOfGuiZhou = { R.array.guiyang_city_item,
  187. R.array.lupanshui_city_item, R.array.zhunyi_city_item,
  188. R.array.anshun_city_item, R.array.tongren_city_item,
  189. R.array.qingxinan_city_item, R.array.biji_city_item,
  190. R.array.qingdongnan_city_item, R.array.qingnan_city_item };
  191. private int[] countyOfYunNan = { R.array.kunming_city_item,
  192. R.array.qujing_city_item, R.array.yuexi_city_item,
  193. R.array.baoshan_city_item, R.array.zhaotong_city_item,
  194. R.array.lijiang_city_item, R.array.simao_city_item,
  195. R.array.lingcang_city_item, R.array.chuxiong_city_item,
  196. R.array.honghe_city_item, R.array.wenshan_city_item,
  197. R.array.xishuangbanna_city_item, R.array.dali_city_item,
  198. R.array.dehuang_city_item, R.array.nujiang_city_item,
  199. R.array.diqing_city_item };
  200. private int[] countyOfXiZang = { R.array.lasa_city_item,
  201. R.array.changdu_city_item, R.array.shannan_city_item,
  202. R.array.rgeze_city_item, R.array.naqu_city_item,
  203. R.array.ali_city_item, R.array.linzhi_city_item };
  204. private int[] countyOfShanXi2 = { R.array.xian_city_item,
  205. R.array.tongchuan_city_item, R.array.baoji_city_item,
  206. R.array.xianyang_city_item, R.array.weinan_city_item,
  207. R.array.yanan_city_item, R.array.hanzhong_city_item,
  208. R.array.yulin_city_item, R.array.ankang_city_item,
  209. R.array.shangluo_city_item };
  210. private int[] countyOfGanSu = { R.array.lanzhou_city_item,
  211. R.array.jiayuguan_city_item, R.array.jinchang_city_item,
  212. R.array.baiyin_city_item, R.array.tianshui_city_item,
  213. R.array.wuwei_city_item, R.array.zhangyue_city_item,
  214. R.array.pingliang_city_item, R.array.jiuquan_city_item,
  215. R.array.qingyang_city_item, R.array.dingxi_city_item,
  216. R.array.longnan_city_item, R.array.linxia_city_item,
  217. R.array.gannan_city_item };
  218. private int[] countyOfQingHai = { R.array.xining_city_item,
  219. R.array.haidong_city_item, R.array.haibai_city_item,
  220. R.array.huangnan_city_item, R.array.hainan_city_item,
  221. R.array.guluo_city_item, R.array.yushu_city_item,
  222. R.array.haixi_city_item };
  223. private int[] countyOfNingXia = { R.array.yinchuan_city_item,
  224. R.array.shizuishan_city_item, R.array.wuzhong_city_item,
  225. R.array.guyuan_city_item, R.array.zhongwei_city_item };
  226. private int[] countyOfXinJiang = { R.array.wulumuqi_city_item,
  227. R.array.kelamayi_city_item, R.array.tulyfan_city_item,
  228. R.array.hami_city_item, R.array.changji_city_item,
  229. R.array.boertala_city_item, R.array.bayinguolen_city_item,
  230. R.array.akesu_city_item, R.array.kemuleisu_city_item,
  231. R.array.geshen_city_item, R.array.hetian_city_item,
  232. R.array.yili_city_item, R.array.tacheng_city_item,
  233. R.array.aleitai_city_item, R.array.shihezi_city_item,
  234. R.array.alaer_city_item, R.array.tumushihe_city_item,
  235. R.array.wujiaqu_city_item };
  236. private int[] countyOfHongKong = {};
  237. private int[] countyOfAoMen = {};
  238. private int[] countyOfTaiWan = {};
  239. private ArrayAdapter<CharSequence> province_adapter;
  240. private ArrayAdapter<CharSequence> city_adapter;
  241. private ArrayAdapter<CharSequence> county_adapter;
  242. @Override
  243. public void onCreate(Bundle savedInstanceState) {
  244. super.onCreate(savedInstanceState);
  245. setContentView(R.layout.activity_main);
  246. loadSpinner();
  247. }
  248. private void loadSpinner() {
  249. display = (EditText) findViewById(R.id.display_edit);
  250. province_spinner = (Spinner) findViewById(R.id.province_spinner);
  251. //绑定省份的数据
  252. province_spinner.setPrompt("请选择省份");
  253. province_adapter = ArrayAdapter.createFromResource(this,
  254. R.array.province_item, android.R.layout.simple_spinner_item);
  255. province_adapter
  256. .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  257. province_spinner.setAdapter(province_adapter);
  258. // select(province_spinner, province_adapter, R.array.province_item);
  259. //添加监听,一开始的时候城市,县区的内容是不显示的而是根据省的内容进行联动
  260. province_spinner
  261. .setOnItemSelectedListener(new OnItemSelectedListener() {
  262. @Override
  263. public void onItemSelected(AdapterView<?> arg0, View arg1,
  264. int arg2, long arg3) {
  265. provinceId = province_spinner.getSelectedItemPosition();
  266. strProvince = province_spinner.getSelectedItem()
  267. .toString();//得到选择的内容,也就是省的名字
  268. city_spinner = (Spinner) findViewById(R.id.city_spinner);
  269. if (true) {
  270. System.out.println("province: "
  271. + province_spinner.getSelectedItem()
  272. .toString() + provinceId.toString());
  273. county_spinner = (Spinner) findViewById(R.id.county_spinner);
  274. city_spinner = (Spinner) findViewById(R.id.city_spinner);
  275. city_spinner.setPrompt("请选择城市");//设置标题
  276. select(city_spinner, city_adapter, city[provinceId]);//城市一级的数据绑定
  277. /*通过这个city[provinceId]指明了该省市的City集合
  278. * R。array.beijing*/
  279. city_spinner
  280. .setOnItemSelectedListener(new OnItemSelectedListener() {
  281. @Override
  282. public void onItemSelected(
  283. AdapterView<?> arg0, View arg1,
  284. int arg2, long arg3) {
  285. cityId = city_spinner
  286. .getSelectedItemPosition();//得到city的id
  287. strCity = city_spinner
  288. .getSelectedItem()
  289. .toString();//得到city的内容
  290. Log.v("test""city: "
  291. + city_spinner
  292. .getSelectedItem()
  293. .toString()//输出测试一下
  294. + cityId.toString());
  295. if (true) {
  296. //这里开始设置县区一级的内容
  297. county_spinner = (Spinner) findViewById(R.id.county_spinner);
  298. county_spinner
  299. .setPrompt("请选择县区");
  300. switch (provinceId) {
  301. case 0:
  302. select(county_spinner,
  303. county_adapter,
  304. countyOfBeiJing[cityId]);
  305. break;
  306. case 1:
  307. select(county_spinner,
  308. county_adapter,
  309. countyOfTianJing[cityId]);
  310. break;
  311. case 2:
  312. select(county_spinner,
  313. county_adapter,
  314. countyOfHeBei[cityId]);
  315. break;
  316. case 3:
  317. select(county_spinner,
  318. county_adapter,
  319. countyOfShanXi1[cityId]);
  320. break;
  321. case 4:
  322. select(county_spinner,
  323. county_adapter,
  324. countyOfNeiMengGu[cityId]);
  325. break;
  326. case 5:
  327. select(county_spinner,
  328. county_adapter,
  329. countyOfLiaoNing[cityId]);
  330. break;
  331. case 6:
  332. select(county_spinner,
  333. county_adapter,
  334. countyOfJiLin[cityId]);
  335. break;
  336. case 7:
  337. select(county_spinner,
  338. county_adapter,
  339. countyOfHeiLongJiang[cityId]);
  340. break;
  341. case 8:
  342. select(county_spinner,
  343. county_adapter,
  344. countyOfShangHai[cityId]);
  345. break;
  346. case 9:
  347. select(county_spinner,
  348. county_adapter,
  349. countyOfJiangSu[cityId]);
  350. break;
  351. case 10:
  352. select(county_spinner,
  353. county_adapter,
  354. countyOfZheJiang[cityId]);
  355. break;
  356. case 11:
  357. select(county_spinner,
  358. county_adapter,
  359. countyOfAnHui[cityId]);
  360. break;
  361. case 12:
  362. select(county_spinner,
  363. county_adapter,
  364. countyOfFuJian[cityId]);
  365. break;
  366. case 13:
  367. select(county_spinner,
  368. county_adapter,
  369. countyOfJiangXi[cityId]);
  370. break;
  371. case 14:
  372. select(county_spinner,
  373. county_adapter,
  374. countyOfShanDong[cityId]);
  375. break;
  376. case 15:
  377. select(county_spinner,
  378. county_adapter,
  379. countyOfHeNan[cityId]);
  380. break;
  381. case 16:
  382. select(county_spinner,
  383. county_adapter,
  384. countyOfHuBei[cityId]);
  385. break;
  386. case 17:
  387. select(county_spinner,
  388. county_adapter,
  389. countyOfHuNan[cityId]);
  390. break;
  391. case 18:
  392. select(county_spinner,
  393. county_adapter,
  394. countyOfGuangDong[cityId]);
  395. break;
  396. case 19:
  397. select(county_spinner,
  398. county_adapter,
  399. countyOfGuangXi[cityId]);
  400. break;
  401. case 20:
  402. select(county_spinner,
  403. county_adapter,
  404. countyOfHaiNan[cityId]);
  405. break;
  406. case 21:
  407. select(county_spinner,
  408. county_adapter,
  409. countyOfChongQing[cityId]);
  410. break;
  411. case 22:
  412. select(county_spinner,
  413. county_adapter,
  414. countyOfSiChuan[cityId]);
  415. break;
  416. case 23:
  417. select(county_spinner,
  418. county_adapter,
  419. countyOfGuiZhou[cityId]);
  420. break;
  421. case 24:
  422. select(county_spinner,
  423. county_adapter,
  424. countyOfYunNan[cityId]);
  425. break;
  426. case 25:
  427. select(county_spinner,
  428. county_adapter,
  429. countyOfXiZang[cityId]);
  430. break;
  431. case 26:
  432. select(county_spinner,
  433. county_adapter,
  434. countyOfShanXi2[cityId]);
  435. break;
  436. case 27:
  437. select(county_spinner,
  438. county_adapter,
  439. countyOfGanSu[cityId]);
  440. break;
  441. case 28:
  442. select(county_spinner,
  443. county_adapter,
  444. countyOfQingHai[cityId]);
  445. break;
  446. case 29:
  447. select(county_spinner,
  448. county_adapter,
  449. countyOfNingXia[cityId]);
  450. break;
  451. case 30:
  452. select(county_spinner,
  453. county_adapter,
  454. countyOfXinJiang[cityId]);
  455. break;
  456. case 31:
  457. select(county_spinner,
  458. county_adapter,
  459. countyOfHongKong[cityId]);
  460. break;
  461. case 32:
  462. select(county_spinner,
  463. county_adapter,
  464. countyOfAoMen[cityId]);
  465. break;
  466. case 33:
  467. select(county_spinner,
  468. county_adapter,
  469. countyOfTaiWan[cityId]);
  470. break;
  471. default:
  472. break;
  473. }
  474. county_spinner
  475. .setOnItemSelectedListener(new OnItemSelectedListener() {
  476. @Override
  477. public void onItemSelected(
  478. AdapterView<?> arg0,
  479. View arg1,
  480. int arg2,
  481. long arg3) {
  482. strCounty = county_spinner
  483. .getSelectedItem()
  484. .toString();
  485. display.setText(strProvince
  486. "-"
  487. + strCity
  488. "-"
  489. + strCounty);
  490. }
  491. @Override
  492. public void onNothingSelected(
  493. AdapterView<?> arg0) {
  494. }
  495. });
  496. }
  497. }
  498. @Override
  499. public void onNothingSelected(
  500. AdapterView<?> arg0) {
  501. // TODO Auto-generated method stub
  502. }
  503. });
  504. }
  505. }
  506. @Override
  507. public void onNothingSelected(AdapterView<?> arg0) {
  508. }
  509. });
  510. }
  511. /*通过方法动态的添加适配器*/
  512. private void select(Spinner spin, ArrayAdapter<CharSequence> adapter,
  513. int arry) {
  514. //注意这里的arry不仅仅但是一个整形,他代表了一个数组!
  515. adapter = ArrayAdapter.createFromResource(this, arry,
  516. android.R.layout.simple_spinner_item);
  517. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  518. spin.setAdapter(adapter);
  519. // spin.setSelection(0,true);
  520. }
0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6407次
    • 积分:142
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:16篇
    • 译文:0篇
    • 评论:1条
    文章分类