咱們都知道在IOS裏面有個滾輪選擇器,在Android中有人也實現了一個相似的,叫android-wheel。不過我的感受實現的仍是有點粗糙,這個後面再說。android-wheel給出了好幾個不一樣類型的demo,不過裏面的demo有個時間選擇器(年月日選擇)。沒用過的能夠去看看裏面的demo,應對通常的都沒什麼大礙。因爲公司項目要省、市、地區選擇的功能,還要和IOS日期選擇器同樣,因此就研究了下android-wheel。下面說說我是如何實現省、市、地區選擇的功能,還望你們指出其中的不足一塊兒討論。java
首先是xml佈局 (Activity_main.xml)android
數據庫裏面的數據是有必定查詢規則的,如圖所示:git
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的值。github
下面是主Activity文件sql
package com.wheeltest; 數據庫
import java.io.IOException; 數組
import java.util.Map; app
import kankan.wheel.widget.OnWheelScrollListener; ide
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你們能夠看看,有問題也歡迎提出來。最後附上一張效果圖。