项目源码地址:https://fengsourcecode.googlecode.com/svn/trunk/ProvinceAndCity
1.首先是bean 省的
/** * @(#) Province.java Created on 2012-6-13 * * Copyright (c) 2012 Aspire. All Rights Reserved */ package com.android.test.provinceandcity.bean; import java.util.List; /** * The class <code>Province</code> * * @author ouyangfeng * @version 1.0 */ public class Province { /** * province name */ private String name; /** * province code */ private int code; /** * province have cities */ private List<City> cities; public Province(String name, int code) { super(); this.name = name; this.code = code; } public Province(String name, int code, List<City> cities) { super(); this.name = name; this.code = code; this.cities = cities; } /** * Getter of name * * @return the name */ public String getName() { return name; } /** * Setter of name * * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * Getter of code * * @return the code */ public int getCode() { return code; } /** * Setter of code * * @param code * the code to set */ public void setCode(int code) { this.code = code; } /** * Getter of cities * * @return the cities */ public List<City> getCities() { return cities; } /** * Setter of cities * * @param cities * the cities to set */ public void setCities(List<City> cities) { this.cities = cities; } /** * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return name; } }
然后是市的
/** * @(#) City.java Created on 2012-6-13 * * Copyright (c) 2012 Aspire. All Rights Reserved */ package com.android.test.provinceandcity.bean; /** * The class <code>City</code> * * @author ouyangfeng * @version 1.0 */ public class City { /** * city name */ private String name; /** * province code; */ private int province_code; /** * city code */ private int code; /** * city unique code */ private int unique_code; public City(String name, int province_code, int code, int unique_code) { super(); this.name = name; this.province_code = province_code; this.code = code; this.unique_code = unique_code; } /** * Getter of name * * @return the name */ public String getName() { return name; } /** * Setter of name * * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * Getter of province_code * * @return the province_code */ public int getProvince_code() { return province_code; } /** * Setter of province_code * * @param province_code * the province_code to set */ public void setProvince_code(int province_code) { this.province_code = province_code; } /** * Getter of code * * @return the code */ public int getCode() { return code; } /** * Setter of code * * @param code * the code to set */ public void setCode(int code) { this.code = code; } /** * Getter of unique_code * * @return the unique_code */ public int getUnique_code() { return unique_code; } /** * Setter of unique_code * * @param unique_code * the unique_code to set */ public void setUnique_code(int unique_code) { this.unique_code = unique_code; } /** * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return name; } }
然后是关于数据解析的
/** * @(#) ProvinceParse.java Created on 2012-6-13 * * Copyright (c) 2012 Aspire. All Rights Reserved */ package com.android.test.provinceandcity.parse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import android.content.Context; import com.android.test.provinceandcity.bean.City; import com.android.test.provinceandcity.bean.Province; /** * The class <code>ProvinceParse</code> * * @author ouyangfeng * @version 1.0 */ public class ProvinceParse { private static final String SPLIT_REGEX = ","; private Context mContext; private int province_id; private int cities_id; private List<Province> provinces; private ProvinceParse() { } public static ProvinceParse build(Context mContext, int province_id, int cities_id) { final ProvinceParse parse = new ProvinceParse(); parse.mContext = mContext; parse.province_id = province_id; parse.cities_id = cities_id; parse.parse(); return parse; } /** * parse from file */ private void parse() { try { parseProvince(); final List<City> cities = parseCity(); List<City> tempCities = null; for (Province province : provinces) { tempCities = new ArrayList<City>(); for (City city : cities) { if (city.getProvince_code() == province.getCode()) { tempCities.add(city); } } province.setCities(tempCities); } System.out.println(); } catch (IOException e) { e.printStackTrace(); } } private void parseProvince() throws IOException { final List<String> strings = readLine(mContext, province_id); provinces = new ArrayList<Province>(); Province province = null; String[] splitstr = null; for (String str : strings) { splitstr = splitLine(str, SPLIT_REGEX); if (splitstr.length == 2) { province = new Province(splitstr[0], Integer.parseInt(splitstr[1])); provinces.add(province); } } } private List<City> parseCity() throws IOException { final List<String> strings = readLine(mContext, cities_id); final List<City> cities = new ArrayList<City>(); City city = null; String[] splitstr = null; for (String str : strings) { splitstr = splitLine(str, SPLIT_REGEX); if (splitstr.length == 4) { city = new City(splitstr[1], Integer.parseInt(splitstr[0]), Integer.parseInt(splitstr[2]), Integer.parseInt(splitstr[3])); cities.add(city); } } return cities; } /** * Getter of provinces * * @return the provinces */ public List<Province> getProvinces() { return provinces; } private static String[] splitLine(String str, String regex) { return str.split(regex); } /** * read file by read line * * @param mContext * @param id * @return * @throws IOException */ private static List<String> readLine(Context mContext, int id) throws IOException { final InputStream in = mContext.getResources().openRawResource(id); final BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK")); final List<String> strings = new ArrayList<String>(); String line = null; while (null != (line = reader.readLine())) { strings.add(line); } reader.close(); return strings; } }
最后activity的
package com.android.test.provinceandcity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import com.android.test.provinceandcity.bean.City; import com.android.test.provinceandcity.bean.Province; import com.android.test.provinceandcity.parse.ProvinceParse; public class ProvinceActivity extends Activity implements OnClickListener { private ProvinceParse parse; private Spinner spinner1, spinner2; private Province currentProvince; private City currentCity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } /** * */ private void init() { parse = ProvinceParse.build(this, R.raw.province, R.raw.cities); spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); findViewById(R.id.button1).setOnClickListener(this); ArrayAdapter<Province> provinceAdapter = new ArrayAdapter<Province>(this, R.layout.simple_spinner_item, android.R.id.text1, parse.getProvinces()); spinner1.setAdapter(provinceAdapter); spinner1.setOnItemSelectedListener(new ProvinceAdapter()); spinner2.setOnItemSelectedListener(new CityAdapter()); } public void onProvinChange(int position) { currentProvince = parse.getProvinces().get(position); ArrayAdapter<City> cityAdapter = new ArrayAdapter<City>(this, R.layout.simple_spinner_item, android.R.id.text1, currentProvince.getCities()); spinner2.setAdapter(cityAdapter); } class ProvinceAdapter implements OnItemSelectedListener { /** * (non-Javadoc) * * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, * android.view.View, int, long) */ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { onProvinChange(position); } /** * (non-Javadoc) * * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView) */ @Override public void onNothingSelected(AdapterView<?> parent) { } } final class CityAdapter extends ProvinceAdapter { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { currentCity = currentProvince.getCities().get(position); } } /** * (non-Javadoc) * * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(View v) { Toast.makeText(this, "" + currentProvince + currentCity, Toast.LENGTH_SHORT).show(); } }
两个xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <!-- /* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml ** ** Copyright 2006, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:textSize="15pt" />