标签:
1 package com.example.homeworkday04_2; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.LinkedHashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import android.os.Bundle; 10 import android.R.string; 11 import android.app.Activity; 12 import android.util.Log; 13 import android.view.View; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemSelectedListener; 16 import android.widget.ArrayAdapter; 17 import android.widget.Spinner; 18 19 public class MainActivity extends Activity { 20 private Spinner provinceSpinner = null; // 省级(省、直辖市) 21 private Spinner citySpinner = null; // 地级市 22 private Spinner countySpinner = null; // 县级(区、县、县级市) 23 ArrayAdapter<String> provinceAdapter = null; // 省级适配器 24 ArrayAdapter<String> cityAdapter = null; // 地级适配器 25 ArrayAdapter<String> countyAdapter = null; // 县级适配器 26 private Map<String, Map<String, ArrayList<String>>> map = new HashMap<String, Map<String, ArrayList<String>>>(); 27 Map<String, ArrayList<String>> map2 = new HashMap<String, ArrayList<String>>(); 28 Map<String, ArrayList<String>> map3 = new HashMap<String, ArrayList<String>>(); 29 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 ArrayList<String> counties = new ArrayList<String>(); 34 counties.add("郑州a区"); 35 counties.add("郑州b区"); 36 counties.add("郑州c区"); 37 map2.put("郑州", counties); 38 ArrayList<String> counties1 = new ArrayList<String>(); 39 counties1.add("洛阳a区"); 40 counties1.add("洛阳b区"); 41 counties1.add("洛阳c区"); 42 map2.put("洛阳", counties1); 43 map.put("河南", map2); 44 ArrayList<String> counties2 = new ArrayList<String>(); 45 counties2.add("保定a区"); 46 counties2.add("保定b区"); 47 counties2.add("保定c区"); 48 map3.put("保定", counties2); 49 ArrayList<String> counties3 = new ArrayList<String>(); 50 counties3.add("石家庄a区"); 51 counties3.add("石家庄b区"); 52 counties3.add("石家庄区c"); 53 map3.put("石家庄", counties3); 54 map.put("河北", map3); 55 provinceSpinner = (Spinner) findViewById(R.id.spinner1); 56 citySpinner = (Spinner) findViewById(R.id.spinner2); 57 countySpinner = (Spinner) findViewById(R.id.spinner3); 58 // 省市联动 59 List<String> provicesList = new ArrayList<String>(map.keySet()); 60 provinceAdapter = new ArrayAdapter<String>(this, 61 android.R.layout.simple_spinner_item, provicesList); 62 provinceSpinner.setAdapter(provinceAdapter); 63 provinceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 64 65 @Override 66 public void onItemSelected(AdapterView<?> parent, View view, 67 int position, long id) { 68 // TODO Auto-generated method stub 69 String string = (String) parent.getItemAtPosition(position); 70 map2 = map.get(string);// 获得市县级联动map 这里可以重写一个map 因为省市的原因就拿来用了 71 // 他把原来的 覆盖了 72 List<String> cityList = new ArrayList<String>(map2.keySet());// 或得市列表 73 cityAdapter = new ArrayAdapter<String>(MainActivity.this, 74 android.R.layout.simple_spinner_item, cityList); 75 citySpinner.setAdapter(cityAdapter); 76 77 } 78 79 @Override 80 public void onNothingSelected(AdapterView<?> parent) { 81 // TODO Auto-generated method stub 82 83 } 84 }); 85 // 市县级联动监听 86 citySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 87 88 @Override 89 public void onItemSelected(AdapterView<?> parent, View view, 90 int position, long id) { 91 // TODO Auto-generated method stub 92 String string = (String) parent.getItemAtPosition(position); 93 System.out.println(string); 94 List<String> countylList = map2.get(string); 95 System.out.println(countylList.toString()); 96 97 countyAdapter = new ArrayAdapter<String>(MainActivity.this, 98 android.R.layout.simple_spinner_item, countylList); 99 countySpinner.setAdapter(countyAdapter); 100 101 } 102 103 @Override 104 public void onNothingSelected(AdapterView<?> parent) { 105 // TODO Auto-generated method stub 106 107 } 108 }); 109 110 } 111 112 }
标签:
原文地址:http://www.cnblogs.com/xiaoxiaoshen/p/5115633.html