项目源码地址: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" />
分享到:
相关推荐
本人亲测,完全可以,完整代码,含layout布局文件代码
Android spinner 案例Android spinner 案例Android spinner 案例
在Android的开发中,经常需要添加地址,这里是一个简单的根据xml文件来解析去获取省市地址的联动
android spinner的前面设置图标android spinner的前面设置图标android spinner的前面设置图标android spinner的前面设置图标android spinner的前面设置图标
Android 自定义Spinner样式的 选择框背景,选择框文字属性,弹出框文字属性
android spinner的简单代码 只有几行
Xamarin.Android Spinner的简单示例。包括用法,载入等。
简单的设置spinner默认的标题 例如“请选择”等等
Android Spinner实现图片列表;通常的Spinner只能展示文字列表,此demo实现了Spinner展示图片列表。 支持sdk2.2及以上版本。
利用android中的spinner控件 实现了全国省市三级联动
android spinner控件添加,删除使用源代码 android spinner控件添加,删除使用源代码
对于android项目开发来说,常常会用到Spinner实现下拉框的效果。而对于Spinner加载适配器的方法有多种: 1.直接加载android自带的ArrayAdapter,SimpleAdapter; 2.自定义继承BaseAdapter的适配器。 对于适配器加载...
我们大家都应该知道,android给我们提供了一个spinner控件,这个控件主要就是一个列表。...此代码通过Spinner控件,实现省市县级联显示,比如选择了江苏省,则显示出江苏省下的市,以及市下面的县。
android spinner 自定义 字体大小
利用spinner实现省市二级级联,有注释能执行。是学习、使用的好例子!
可以记住历史选项的Android spinner
Android Spinner控件简单使用方法,图片加文字的展示效果
android Spinner 简单快速的显示
功能:可以设置默认值,下拉选框自定义,显示框自定义 这是一个自定义控件使用非常简单,复制粘贴到项目可直接使用,可高度自定义
在这个例子中包括了众多Android Spinner 控件的用法,以一个下拉框的效果来展示很多Spinner 的使用技巧,示例中的myspinner_dropdown自定义下拉菜单模式定义在res/layout目录下,代码中演示了将ArrayAdapter添加...