标签:
1 package com.lixu.duocengxiala; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.Activity; 7 import android.content.Context; 8 import android.os.Bundle; 9 import android.view.LayoutInflater; 10 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.AdapterView; 14 import android.widget.AdapterView.OnItemSelectedListener; 15 import android.widget.ArrayAdapter; 16 import android.widget.Spinner; 17 import android.widget.TextView; 18 19 public class MainActivity extends Activity { 20 private static final String CITY = "city"; 21 ArrayList<String> citys = null; 22 ArrayList<String> qu = null; 23 int p = 0; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 String[] sheng = this.getResources().getStringArray(R.array.sheng); 31 final String[] city_sichuan = this.getResources().getStringArray(R.array.city_sichuan); 32 final String[] city_guangdong = this.getResources().getStringArray(R.array.city_guangdong); 33 final String[] city_shanxi = this.getResources().getStringArray(R.array.city_shanxi); 34 final String[] qu_chengdu = this.getResources().getStringArray(R.array.qu_chengdu); 35 final String[] qu_mianyang = this.getResources().getStringArray(R.array.qu_mianyang); 36 final String[] qu_guangyuan = this.getResources().getStringArray(R.array.qu_guangyuan); 37 final String[] qu_taiyuan = this.getResources().getStringArray(R.array.qu_taiyuan); 38 final String[] qu_guangzhou = this.getResources().getStringArray(R.array.qu_guangzhou); 39 40 citys = new ArrayList<String>(); 41 qu = new ArrayList<String>(); 42 43 Spinner sp1 = (Spinner) findViewById(R.id.sp1); 44 Spinner sp2 = (Spinner) findViewById(R.id.sp2); 45 Spinner sp3 = (Spinner) findViewById(R.id.sp3); 46 47 final MyAdapter1 mMyAdapter1 = new MyAdapter1(this, -1, sheng); 48 final MyAdapter2 mMyAdapter2 = new MyAdapter2(this, -1, citys); 49 final MyAdapter3 mMyAdapter3 = new MyAdapter3(this, -1, qu); 50 51 sp1.setAdapter(mMyAdapter1); 52 sp2.setAdapter(mMyAdapter2); 53 sp3.setAdapter(mMyAdapter3); 54 55 sp1.setOnItemSelectedListener(new OnItemSelectedListener() { 56 57 @Override 58 public void onItemSelected(AdapterView<?> arg0, View arg1, int possheng, long arg3) { 59 p = possheng; 60 switch (possheng) { 61 case 0: 62 citys.clear(); 63 for (String n : city_sichuan) 64 citys.add(n); 65 mMyAdapter2.notifyDataSetChanged(); 66 67 break; 68 case 1: 69 citys.clear(); 70 for (String n : city_shanxi) 71 citys.add(n); 72 mMyAdapter2.notifyDataSetChanged(); 73 74 break; 75 case 2: 76 77 citys.clear(); 78 for (String n : city_guangdong) 79 citys.add(n); 80 mMyAdapter2.notifyDataSetChanged(); 81 break; 82 83 default: 84 break; 85 } 86 } 87 88 @Override 89 public void onNothingSelected(AdapterView<?> arg0) { 90 91 } 92 }); 93 sp2.setOnItemSelectedListener(new OnItemSelectedListener() { 94 95 @Override 96 public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) { 97 98 switch (pos) { 99 case 0: 100 if (p == 0) { 101 102 qu.clear(); 103 104 for (String n : qu_chengdu) 105 qu.add(n); 106 mMyAdapter3.notifyDataSetChanged(); 107 108 } else if (p == 1) { 109 qu.clear(); 110 for (String n : qu_taiyuan) 111 qu.add(n); 112 mMyAdapter3.notifyDataSetChanged(); 113 } else if (p == 2) { 114 qu.clear(); 115 for (String n : qu_guangzhou) 116 qu.add(n); 117 mMyAdapter3.notifyDataSetChanged(); 118 } 119 120 break; 121 case 1: 122 if (p == 0) { 123 qu.clear(); 124 for (String n : qu_mianyang) 125 qu.add(n); 126 mMyAdapter3.notifyDataSetChanged(); 127 } 128 break; 129 case 2: 130 if (p == 0) { 131 qu.clear(); 132 for (String n : qu_guangyuan) 133 qu.add(n); 134 mMyAdapter3.notifyDataSetChanged(); 135 } 136 break; 137 138 default: 139 break; 140 } 141 } 142 143 @Override 144 public void onNothingSelected(AdapterView<?> arg0) { 145 146 } 147 }); 148 149 } 150 151 private class MyAdapter1 extends ArrayAdapter { 152 private Context context; 153 private String[] sheng; 154 private LayoutInflater flater; 155 156 public MyAdapter1(Context context, int resource, String[] sheng) { 157 super(context, resource, sheng); 158 this.context = context; 159 this.sheng = sheng; 160 flater = LayoutInflater.from(context); 161 } 162 163 @Override 164 public int getCount() { 165 return sheng.length; 166 } 167 168 @Override 169 public View getView(int position, View convertView, ViewGroup parent) { 170 if (convertView == null) 171 convertView = flater.inflate(R.layout.list, null); 172 173 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 174 tv.setText(sheng[position]); 175 176 return convertView; 177 } 178 179 @Override 180 public View getDropDownView(int position, View convertView, ViewGroup parent) { 181 if (convertView == null) 182 convertView = flater.inflate(R.layout.list, null); 183 184 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 185 tv.setText(sheng[position]); 186 187 return convertView; 188 189 } 190 191 } 192 193 private class MyAdapter2 extends ArrayAdapter { 194 private Context context; 195 private ArrayList<String> citys; 196 private LayoutInflater flater; 197 198 public MyAdapter2(Context context, int resource, ArrayList<String> citys) { 199 super(context, resource, citys); 200 this.context = context; 201 this.citys = citys; 202 flater = LayoutInflater.from(context); 203 } 204 205 @Override 206 public int getCount() { 207 return citys.size(); 208 } 209 210 @Override 211 public View getView(int position, View convertView, ViewGroup parent) { 212 if (convertView == null) 213 convertView = flater.inflate(R.layout.list, null); 214 215 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 216 tv.setText(citys.get(position) + ""); 217 218 return convertView; 219 } 220 221 @Override 222 public View getDropDownView(int position, View convertView, ViewGroup parent) { 223 if (convertView == null) 224 convertView = flater.inflate(R.layout.list, null); 225 226 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 227 tv.setText(citys.get(position)); 228 229 return convertView; 230 231 } 232 233 } 234 235 private class MyAdapter3 extends ArrayAdapter { 236 private Context context; 237 private ArrayList<String> qu; 238 private LayoutInflater flater; 239 240 public MyAdapter3(Context context, int resource, ArrayList<String> qu) { 241 super(context, resource, qu); 242 this.context = context; 243 this.qu = qu; 244 flater = LayoutInflater.from(context); 245 } 246 247 @Override 248 public int getCount() { 249 return qu.size(); 250 } 251 252 @Override 253 public View getView(int position, View convertView, ViewGroup parent) { 254 if (convertView == null) 255 convertView = flater.inflate(R.layout.list, null); 256 257 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 258 tv.setText(qu.get(position)); 259 260 return convertView; 261 } 262 263 @Override 264 public View getDropDownView(int position, View convertView, ViewGroup parent) { 265 if (convertView == null) 266 convertView = flater.inflate(R.layout.list, null); 267 268 TextView tv = (TextView) convertView.findViewById(R.id.tv1); 269 tv.setText(qu.get(position)); 270 271 return convertView; 272 273 } 274 275 } 276 277 }
vales XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string-array name="sheng"> 5 <item>四川省</item> 6 <item>山西省</item> 7 <item>广东省</item> 8 </string-array> 9 10 <string-array name="city_sichuan"> 11 <item>成都</item> 12 <item>绵阳</item> 13 <item>广元</item> 14 <item>巴中</item> 15 <item>乐山</item> 16 <item>眉山</item> 17 </string-array> 18 19 <string-array name="city_shanxi"> 20 <item>太原</item> 21 <item>晋中</item> 22 <item>介休</item> 23 <item>大同</item> 24 <item>运城</item> 25 <item>长治</item> 26 </string-array> 27 28 <string-array name="city_guangdong"> 29 <item>广州</item> 30 <item>深圳</item> 31 <item>东莞</item> 32 <item>云浮</item> 33 <item>珠海</item> 34 <item>湛江</item> 35 </string-array> 36 37 <string-array name="qu_chengdu"> 38 <item>金牛区</item> 39 <item>青羊区</item> 40 <item>成华区</item> 41 <item>高新区</item> 42 <item>武侯区</item> 43 <item>锦江区</item> 44 </string-array> 45 46 <string-array name="qu_taiyuan"> 47 <item>迎泽区</item> 48 <item>万柏林区</item> 49 <item>杏花岭区</item> 50 <item>小店区</item> 51 <item>尖草坪区</item> 52 <item>晋源区</item> 53 </string-array> 54 55 <string-array name="qu_guangzhou"> 56 <item>荔湾区</item> 57 <item>海珠区</item> 58 <item>越秀区</item> 59 <item>天河区</item> 60 <item>白云区</item> 61 <item>花都区</item> 62 </string-array> 63 64 <string-array name="qu_mianyang"> 65 <item>培城区</item> 66 <item>游仙区</item> 67 </string-array> 68 69 <string-array name="qu_guangyuan"> 70 <item>旺苍区</item> 71 <item>剑阁区</item> 72 </string-array> 73 74 </resources>
xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="horizontal" 7 tools:context="com.lixu.duocengxiala.MainActivity" > 8 9 <Spinner 10 android:id="@+id/sp1" 11 android:layout_weight="1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 /> 15 <Spinner 16 android:id="@+id/sp2" 17 android:layout_weight="1" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 /> 21 <Spinner 22 android:id="@+id/sp3" 23 android:layout_weight="1" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 /> 27 28 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>
程序运行效果图:
标签:
原文地址:http://www.cnblogs.com/labixiaoxin/p/4977113.html