实现该功能主要有两个难点:
1.XML文件的Pull解析。由于XML文件的结构较为复杂,所以解析时要注意,划分XML文件的结构,根据结构建立相应的和集合。本例中需要建立三个类和三个对象。三个类分别为,省份类,城市类,和地区类。三个集合分别为,省份集合,城市集合,和地区集合。省份类里有该省份的城市集合,城市类里有该城市所有地区的集合,然后将所有的省份放入省份集合当中。
2.三级联动的实现方法:在Spinner1的监听选择事件中,根据用户选择的省份(position)获得该省份所对应的城市(list.get(position).getCitys()),设置Spinner2的数据源。在Spinner2的监听选择事件中,根据用户选择的城市(position)获得该城市所对应的所有地区(province.getCitys().get(position).getDistricts()),设置Spinner3的数据源。
下面为代码部分
省份类代码:
import java.util.*; public class Province {//省份内 private String id; private String name; private List<City> citys; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<City> getCitys() { return citys; } public void setCitys(List<City> citys) { this.citys = citys; } @Override public String toString() { return name; } }
地区类代码
/** * Created by hjw on 2016/5/14. */ public class District {//地区类 private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
城市类代码
package com.example.hjw.androidday043; /** * Created by hjw on 2016/5/14. */ import java.util.ArrayList; import java.util.List; public class City {//城市类 private String id; private String name; private List<District> districts; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<District> getDistricts() { return districts; } public void setDistricts(List<District> districts) { this.districts = districts; } @Override public String toString() { return name; } }
主活动代码
package com.example.hjw.androidday043; import android.content.res.XmlResourceParser; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Spinner spinner1,spinner2,spinner3; private Province province = null; private List<Province> list = new ArrayList<Province>(); ArrayAdapter<Province> arrayAdapter1; ArrayAdapter<City> arrayAdapter2; ArrayAdapter<District>arrayAdapter3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner3 = (Spinner)findViewById(R.id.s3); spinner2 = (Spinner)findViewById(R.id.s2); spinner1 = (Spinner)findViewById(R.id.s1); list= parser(); arrayAdapter1 = new ArrayAdapter<Province>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list); arrayAdapter2 = new ArrayAdapter<City>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list.get(0).getCitys()); arrayAdapter3 = new ArrayAdapter<District>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list.get(0).getCitys().get(0).getDistricts()); spinner1.setAdapter(arrayAdapter1); spinner1.setSelection(0, true); spinner2.setAdapter(arrayAdapter2); spinner2.setSelection(0, true); spinner3.setAdapter(arrayAdapter3); spinner3.setSelection(0, true); spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { province = list.get(position); arrayAdapter2 = new ArrayAdapter<City>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, list.get(position).getCitys()); spinner2.setAdapter(arrayAdapter2); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { arrayAdapter3 = new ArrayAdapter<District>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,province.getCitys().get(position).getDistricts()); spinner3.setAdapter(arrayAdapter3); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } public List<Province> parser(){ List<Province>list =null; Province province = null; List<City>cities = null; City city = null; List<District>districts = null; District district = null; // 创建解析器,并制定解析的xml文件 XmlResourceParser parser = getResources().getXml(R.xml.cities); try{ int type = parser.getEventType(); while(type!=1) { String tag = parser.getName();//获得标签名 switch (type) { case XmlResourceParser.START_DOCUMENT: list = new ArrayList<Province>(); break; case XmlResourceParser.START_TAG: if ("p".equals(tag)) { province=new Province(); cities = new ArrayList<City>(); int n =parser.getAttributeCount(); for(int i=0 ;i<n;i++){ //获得属性的名和值 String name = parser.getAttributeName(i); String value = parser.getAttributeValue(i); if("p_id".equals(name)){ province.setId(value); } } } if ("pn".equals(tag)){//省名字 province.setName(parser.nextText()); } if ("c".equals(tag)){//城市 city = new City(); districts = new ArrayList<District>(); int n =parser.getAttributeCount(); for(int i=0 ;i<n;i++){ String name = parser.getAttributeName(i); String value = parser.getAttributeValue(i); if("c_id".equals(name)){ city.setId(value); } } } if ("cn".equals(tag)){ city.setName(parser.nextText()); } if ("d".equals(tag)){ district = new District(); int n =parser.getAttributeCount(); for(int i=0 ;i<n;i++){ String name = parser.getAttributeName(i); String value = parser.getAttributeValue(i); if("d_id".equals(name)){ district.setId(value); } } district.setName(parser.nextText()); districts.add(district); } break; case XmlResourceParser.END_TAG: if ("c".equals(tag)){ city.setDistricts(districts); cities.add(city); } if("p".equals(tag)){ province.setCitys(cities); list.add(province); } break; default: break; } type = parser.next(); } }catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
Xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hjw.androidday043.MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="match_parent"> <Spinner android:id="@+id/s1" android:layout_weight="2" android:layout_width="0dp" android:layout_height="wrap_content" android:spinnerMode="dropdown"> </Spinner> <Spinner android:id="@+id/s2" android:layout_weight="2" android:layout_width="0dp" android:layout_height="wrap_content" android:spinnerMode="dropdown"></Spinner> <Spinner android:id="@+id/s3" android:layout_weight="2" android:layout_width="0dp" android:layout_height="wrap_content" android:spinnerMode="dropdown"></Spinner> </LinearLayout> </LinearLayout>
xml文件结构
效果图:
时间: 2024-11-23 18:21:28