我们都知道在IOS里面有个滚轮选择器,在Android中有人也实现了一个类似的,叫android-wheel。不过个人感觉实现的还是有点粗糙,这个后面再说。android-wheel给出了好几个不同类型的demo,不过里面的demo有个时间选择器(年月日选择)。没用过的可以去看看里面的demo,应对一般的都没什么大碍。由于公司项目要省、市、地区选择的功能,还要和IOS日期选择器一样,所以就研究了下android-wheel。下面说说我是如何实现省、市、地区选择的功能,还望大家指出其中的不足一起讨论。
首先是xml布局 (Activity_main.xml)
数据库里面的数据是有一定查询规则的,如图所示:
DQXX03为1,2,3分别代表省(直辖市)、市、地区。所以要获得所有省只需查询DQXX03为1就行了。如果要查询某个省下面的市呢?比如要查询河北省的所有市,可以这么写sql语句 select DQX_DQXX01 from DQXX where DQXX02="河北省"; 这样就查出来了河北省对应的DQX_DQXX01的值为13,然后根据这个值在通过select DQXX01,DQXX02 from DQXX where DQX_DQXX01=13 就查询出来了每个市的对应键DQXX01的值为1301,1302,1303.......,查询地区也是类似的,这个时候根据DQX_DQXX01=1301或者1302.....查询对应的地区。最后省,市,地区连接起来就是DQXX05列里面的值,我们只要查询下就可以知道唯一地址DQXX01的值。
下面是主Activity文件
package com.wheeltest;
import java.io.IOException;
import java.util.Map;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
publicclass MainActivity extends Activity {
privatestaticfinal String TAG = "MainActivity";
privatestaticfinal String DBNAME = "dqxx.sqlite";
privatestaticfinal String TABLE_NAME = "dqxx";
private SQLiteDatabase db;
private Map<String, Integer> provinceMap;
private Map<String, Integer> cityMap;
private Map<String, Integer> areaMap;
private String[] provinceArray;
private String[] cityArray;
private String[] areaArray;
private WheelView provinceWheelView;
private WheelView cityWheelView;
private WheelView areaWheelView;
private ProviceCityAreaAdapter provinceAdapter;
private ProviceCityAreaAdapter cityAdapter;
private ProviceCityAreaAdapter areaAdapter;
private Handler mHandler = new Handler();
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWheelView();
findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View arg0) {
mHandler.postDelayed(new Runnable() {
@Override
publicvoid run() {
StringBuilder sb = new StringBuilder();
sb.append(provinceArray[provinceWheelView.getCurrentItem()]);
if (provinceArray[provinceWheelView.getCurrentItem()].endsWith("市")) {
sb.append("市辖区");
}else {
sb.append(cityArray[cityWheelView.getCurrentItem()]);
}
sb.append(areaArray[areaWheelView.getCurrentItem()]);
Toast.makeText(MainActivity.this, sb.toString()+" key:"+DqxxUtils.findPrimaryKey(db, TABLE_NAME, sb.toString()), Toast.LENGTH_SHORT).show();
}
}, 400);
}
});
}
publicvoid initWheelView() {
provinceWheelView = (WheelView)findViewById(R.id.provice);
cityWheelView = (WheelView)findViewById(R.id.city);
areaWheelView = (WheelView)findViewById(R.id.area);
//初始化省滚轮列表选择器
initProviceMap();
provinceAdapter = new ProviceCityAreaAdapter(MainActivity.this, provinceArray, 0);
provinceWheelView.setViewAdapter(provinceAdapter);
provinceWheelView.setCurrentItem(0);
provinceWheelView.addScrollingListener(privinceScrollListener);
//初始化城市滚轮列表选择器
String provinceName = provinceArray[0];
int dqx_dqxx01 = provinceMap.get(provinceName);
if (provinceName.endsWith("市")) {
initCityMap(dqx_dqxx01, false);
}else {
initCityMap(dqx_dqxx01, true);
}
cityAdapter = new ProviceCityAreaAdapter(MainActivity.this, cityArray, 0);
cityWheelView.setViewAdapter(cityAdapter);
cityWheelView.setCurrentItem(0);
cityWheelView.addScrollingListener(cityScrollListener);
//初始化地区滚轮列表选择器
String cityName = cityArray[0];
int dqx_dqxx01_2 = cityMap.get(cityName);
provinceName = cityArray[0];
if (provinceName.endsWith("市")) {
dqx_dqxx01_2 = dqx_dqxx01_2 * 100 +1;
}
initAreaMap(dqx_dqxx01_2);
areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);
areaWheelView.setViewAdapter(areaAdapter);
areaWheelView.setCurrentItem(0);
}
OnWheelScrollListener privinceScrollListener = new OnWheelScrollListener() {
@Override
publicvoid onScrollingStarted(WheelView wheel) {
}
@Override
publicvoid onScrollingFinished(WheelView wheel) {
int currentItem = wheel.getCurrentItem();
String provinceName = provinceArray[currentItem];
int dqxx01 = provinceMap.get(provinceName);
if (provinceName.endsWith("市")) {
initCityMap(dqxx01, false);
}else {
initCityMap(dqxx01, true);
}
cityAdapter = new ProviceCityAreaAdapter(MainActivity.this, cityArray, 0);
cityWheelView.setViewAdapter(cityAdapter);
cityWheelView.setCurrentItem(0);
String cityName = cityArray[0];
int dqx_dqxx01_2 = cityMap.get(cityName);
if (provinceName.endsWith("市")) {
dqx_dqxx01_2 = dqx_dqxx01_2 * 100 +1;
}
initAreaMap(dqx_dqxx01_2);
areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);
areaWheelView.setViewAdapter(areaAdapter);
areaWheelView.setCurrentItem(0);
}
};
OnWheelScrollListener cityScrollListener = new OnWheelScrollListener() {
@Override
publicvoid onScrollingStarted(WheelView wheel) {
}
@Override
publicvoid onScrollingFinished(WheelView wheel) {
String provinceName = provinceArray[provinceWheelView.getCurrentItem()];
int dqx_dqxx01 = cityMap.get(cityArray[wheel.getCurrentItem()]);
if (provinceName.endsWith("市")) {
dqx_dqxx01 = dqx_dqxx01 * 100 +1;
}
initAreaMap(dqx_dqxx01);
areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);
areaWheelView.setViewAdapter(areaAdapter);
areaWheelView.setCurrentItem(0);
}
};
publicvoid initProviceMap() {
try {
DqxxUtils.copyDB(MainActivity.this, DBNAME);
if (db == null) {
db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);
}
provinceMap = DqxxUtils.getProvince(db, TABLE_NAME);
provinceArray = provinceMap.keySet().toArray(new String[provinceMap.size()]);
} catch (IOException e) {
e.printStackTrace();
}
}
publicvoid initCityMap(int dqx_dqxx01, boolean municipalities) {
try {
DqxxUtils.copyDB(MainActivity.this, DBNAME);
if (db == null) {
db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);
}
cityMap = DqxxUtils.getCity(db, TABLE_NAME, dqx_dqxx01, municipalities);
cityArray = cityMap.keySet().toArray(new String[cityMap.size()]);
} catch (IOException e) {
e.printStackTrace();
}
}
publicvoid initAreaMap(int dqx_dqxx01) {
try {
DqxxUtils.copyDB(MainActivity.this, DBNAME);
if (db == null) {
db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);
}
areaMap = DqxxUtils.getArea(db, TABLE_NAME, dqx_dqxx01);
areaArray = areaMap.keySet().toArray(new String[areaMap.size()]);
} catch (IOException e) {
e.printStackTrace();
}
}
publicclass ProviceCityAreaAdapter extends ArrayWheelAdapter<String> {
privateint currentItem;
privateint currentValue;
public ProviceCityAreaAdapter(Context context, String[] items, int current) {
super(context, items);
this.currentValue = current;
}
publicvoid setCurrentValue(int value){
this.currentValue = value;
}
@Override
protectedvoid configureTextView(TextView view) {
super.configureTextView(view);
// if (currentItem == currentValue) {
// view.setTextColor(0xFF0000F0);
// }
view.setTypeface(Typeface.SANS_SERIF);
}
@Override
public View getItem(int index, View convertView, ViewGroup parent) {
currentItem = index;
returnsuper.getItem(index, convertView, parent);
}
}
@Override
protectedvoid onDestroy() {
if (db != null) {
db.close();
db = null;
}
super.onDestroy();
}
最后一个类是工具类,包含一些数据库操作和文件读写的。我就不贴出来了。我已经把这个工程放到Github上去了地址https://github.com/ywenblocker/Provinces-Picker-wheel大家可以看看,有问题也欢迎提出来。最后附上一张效果图。