android-wheel實現省、市、地區聯動選擇效果

android-wheel實現省、市、地區聯動選擇效果


咱們都知道在IOS裏面有個滾輪選擇器,在Android中有人也實現了一個相似的,叫android-wheel。不過我的感受實現的仍是有點粗糙,這個後面再說。android-wheel給出了好幾個不一樣類型的demo,不過裏面的demo有個時間選擇器(年月日選擇)。沒用過的能夠去看看裏面的demo,應對通常的都沒什麼大礙。因爲公司項目要省、市、地區選擇的功能,還要和IOS日期選擇器同樣,因此就研究了下android-wheel。下面說說我是如何實現省、市、地區選擇的功能,還望你們指出其中的不足一塊兒討論。java

首先是xml佈局 (Activity_main.xml)android


 
  
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_height= "fill_parent"
android:layout_width= "fill_parent"
android:orientation= "vertical"
android:background= "@drawable/layout_bg" >
<LinearLayout
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:layout_gravity= "center_horizontal"
android:paddingTop= "30dp" >
<kankan.wheel.widget.WheelView android:id= "@+id/provice"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:minWidth= "100dp"
/>
<kankan.wheel.widget.WheelView android:id= "@+id/city"
android:layout_height= "wrap_content"
android:layout_width= "100dp" />
<kankan.wheel.widget.WheelView android:id= "@+id/area"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content" />
</LinearLayout>
<Button
android:id= "@+id/btnOK"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_marginTop= "30dp"
android:text= "肯定"
/>
</LinearLayout>
在Activity_main.xml 文件中佈局三個滾輪控件和一個按鈕,三個滾輪分別是省、市、地區選擇滾輪。最後按下按鈕的時候Toast顯示選擇的省市地區和所選擇的地址在數據庫中的key。


數據庫裏面的數據是有必定查詢規則的,如圖所示:git

20130920230238625

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


[java] view plain copy
  1. package com.wheeltest;  數據庫

  2. import java.io.IOException;  數組

  3. import java.util.Map;  app

  4. import kankan.wheel.widget.OnWheelScrollListener;  ide

  5. import kankan.wheel.widget.WheelView;  工具

  6. import kankan.wheel.widget.adapters.ArrayWheelAdapter;  

  7. import android.os.Bundle;  

  8. import android.os.Handler;  

  9. import android.app.Activity;  

  10. import android.content.Context;  

  11. import android.database.sqlite.SQLiteDatabase;  

  12. import android.graphics.Typeface;  

  13. import android.view.View;  

  14. import android.view.ViewGroup;  

  15. import android.widget.TextView;  

  16. import android.widget.Toast;  

  17. publicclass MainActivity extends Activity {  

  18. privatestaticfinal String TAG = "MainActivity";  

  19. privatestaticfinal String DBNAME = "dqxx.sqlite";  

  20. privatestaticfinal String TABLE_NAME = "dqxx";  

  21. private SQLiteDatabase db;  

  22. private Map<String, Integer> provinceMap;  

  23. private Map<String, Integer> cityMap;  

  24. private Map<String, Integer> areaMap;  

  25. private String[] provinceArray;  

  26. private String[] cityArray;  

  27. private String[] areaArray;  

  28. private WheelView provinceWheelView;  

  29. private WheelView cityWheelView;  

  30. private WheelView areaWheelView;  

  31. private ProviceCityAreaAdapter provinceAdapter;  

  32. private ProviceCityAreaAdapter cityAdapter;  

  33. private ProviceCityAreaAdapter areaAdapter;  

  34. private Handler mHandler = new Handler();  

  35. @Override

  36. protectedvoid onCreate(Bundle savedInstanceState) {  

  37. super.onCreate(savedInstanceState);  

  38.        setContentView(R.layout.activity_main);  

  39.        initWheelView();  

  40.        findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {  

  41. @Override

  42. publicvoid onClick(View arg0) {  

  43.                mHandler.postDelayed(new Runnable() {  

  44. @Override

  45. publicvoid run() {  

  46.                        StringBuilder sb = new StringBuilder();  

  47.                        sb.append(provinceArray[provinceWheelView.getCurrentItem()]);  

  48. if (provinceArray[provinceWheelView.getCurrentItem()].endsWith("市")) {  

  49.                            sb.append("市轄區");  

  50.                        }else {  

  51.                            sb.append(cityArray[cityWheelView.getCurrentItem()]);  

  52.                        }  

  53.                        sb.append(areaArray[areaWheelView.getCurrentItem()]);  

  54.                        Toast.makeText(MainActivity.this, sb.toString()+" key:"+DqxxUtils.findPrimaryKey(db, TABLE_NAME, sb.toString()), Toast.LENGTH_SHORT).show();  

  55.                    }  

  56.                }, 400);  

  57.            }  

  58.        });  

  59.    }  

  60. publicvoid initWheelView() {  

  61.        provinceWheelView = (WheelView)findViewById(R.id.provice);  

  62.        cityWheelView = (WheelView)findViewById(R.id.city);  

  63.        areaWheelView = (WheelView)findViewById(R.id.area);  

  64. //初始化省滾輪列表選擇器

  65.        initProviceMap();  

  66.        provinceAdapter = new ProviceCityAreaAdapter(MainActivity.this, provinceArray, 0);  

  67.        provinceWheelView.setViewAdapter(provinceAdapter);  

  68.        provinceWheelView.setCurrentItem(0);  

  69.        provinceWheelView.addScrollingListener(privinceScrollListener);  

  70. //初始化城市滾輪列表選擇器

  71.        String provinceName = provinceArray[0];  

  72. int dqx_dqxx01 = provinceMap.get(provinceName);  

  73. if (provinceName.endsWith("市")) {  

  74.            initCityMap(dqx_dqxx01, false);  

  75.        }else {  

  76.            initCityMap(dqx_dqxx01, true);  

  77.        }  

  78.        cityAdapter = new ProviceCityAreaAdapter(MainActivity.this, cityArray, 0);  

  79.        cityWheelView.setViewAdapter(cityAdapter);  

  80.        cityWheelView.setCurrentItem(0);  

  81.        cityWheelView.addScrollingListener(cityScrollListener);  

  82. //初始化地區滾輪列表選擇器

  83.        String cityName = cityArray[0];  

  84. int dqx_dqxx01_2 = cityMap.get(cityName);  

  85.        provinceName = cityArray[0];  

  86. if (provinceName.endsWith("市")) {  

  87.            dqx_dqxx01_2 = dqx_dqxx01_2 * 100 +1;  

  88.        }  

  89.        initAreaMap(dqx_dqxx01_2);  

  90.        areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);  

  91.        areaWheelView.setViewAdapter(areaAdapter);  

  92.        areaWheelView.setCurrentItem(0);  

  93.    }  

  94.    OnWheelScrollListener privinceScrollListener = new OnWheelScrollListener() {  

  95. @Override

  96. publicvoid onScrollingStarted(WheelView wheel) {  

  97.        }  

  98. @Override

  99. publicvoid onScrollingFinished(WheelView wheel) {  

  100. int currentItem = wheel.getCurrentItem();  

  101.            String provinceName = provinceArray[currentItem];  

  102. int dqxx01 = provinceMap.get(provinceName);  

  103. if (provinceName.endsWith("市")) {  

  104.                initCityMap(dqxx01, false);  

  105.            }else {  

  106.                initCityMap(dqxx01, true);  

  107.            }  

  108.            cityAdapter = new ProviceCityAreaAdapter(MainActivity.this, cityArray, 0);  

  109.            cityWheelView.setViewAdapter(cityAdapter);  

  110.            cityWheelView.setCurrentItem(0);  

  111.            String cityName = cityArray[0];  

  112. int dqx_dqxx01_2 = cityMap.get(cityName);  

  113. if (provinceName.endsWith("市")) {  

  114.                dqx_dqxx01_2 = dqx_dqxx01_2 * 100 +1;  

  115.            }  

  116.            initAreaMap(dqx_dqxx01_2);  

  117.            areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);  

  118.            areaWheelView.setViewAdapter(areaAdapter);  

  119.            areaWheelView.setCurrentItem(0);  

  120.        }  

  121.    };  

  122.    OnWheelScrollListener cityScrollListener = new OnWheelScrollListener() {  

  123. @Override

  124. publicvoid onScrollingStarted(WheelView wheel) {  

  125.        }  

  126. @Override

  127. publicvoid onScrollingFinished(WheelView wheel) {  

  128.            String provinceName = provinceArray[provinceWheelView.getCurrentItem()];  

  129. int dqx_dqxx01 = cityMap.get(cityArray[wheel.getCurrentItem()]);  

  130. if (provinceName.endsWith("市")) {  

  131.                dqx_dqxx01 = dqx_dqxx01 * 100 +1;  

  132.            }  

  133.            initAreaMap(dqx_dqxx01);  

  134.            areaAdapter = new ProviceCityAreaAdapter(MainActivity.this, areaArray, 0);  

  135.            areaWheelView.setViewAdapter(areaAdapter);  

  136.            areaWheelView.setCurrentItem(0);  

  137.        }  

  138.    };  

  139. publicvoid initProviceMap() {  

  140. try {  

  141.            DqxxUtils.copyDB(MainActivity.this, DBNAME);  

  142. if (db == null) {  

  143.                db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);  

  144.            }  

  145.            provinceMap = DqxxUtils.getProvince(db, TABLE_NAME);  

  146.            provinceArray = provinceMap.keySet().toArray(new String[provinceMap.size()]);  

  147.        } catch (IOException e) {  

  148.            e.printStackTrace();  

  149.        }  

  150.    }  

  151. publicvoid initCityMap(int dqx_dqxx01, boolean municipalities) {  

  152. try {  

  153.            DqxxUtils.copyDB(MainActivity.this, DBNAME);  

  154. if (db == null) {  

  155.                db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);  

  156.            }  

  157.            cityMap = DqxxUtils.getCity(db, TABLE_NAME, dqx_dqxx01, municipalities);  

  158.            cityArray = cityMap.keySet().toArray(new String[cityMap.size()]);  

  159.        } catch (IOException e) {  

  160.            e.printStackTrace();  

  161.        }  

  162.    }  

  163. publicvoid initAreaMap(int dqx_dqxx01) {  

  164. try {  

  165.            DqxxUtils.copyDB(MainActivity.this, DBNAME);  

  166. if (db == null) {  

  167.                db = openOrCreateDatabase(getFilesDir().getAbsolutePath() + "/" +DBNAME, Context.MODE_PRIVATE, null);  

  168.            }  

  169.            areaMap = DqxxUtils.getArea(db, TABLE_NAME, dqx_dqxx01);  

  170.            areaArray = areaMap.keySet().toArray(new String[areaMap.size()]);  

  171.        } catch (IOException e) {  

  172.            e.printStackTrace();  

  173.        }  

  174.    }  

  175. publicclass ProviceCityAreaAdapter extends ArrayWheelAdapter<String> {  

  176. privateint currentItem;  

  177. privateint currentValue;  

  178. public ProviceCityAreaAdapter(Context context, String[] items, int current) {  

  179. super(context, items);  

  180. this.currentValue = current;  

  181.        }  

  182. publicvoid setCurrentValue(int value){  

  183. this.currentValue = value;  

  184.        }  

  185. @Override

  186. protectedvoid configureTextView(TextView view) {  

  187. super.configureTextView(view);  

  188. //          if (currentItem == currentValue) {

  189. //              view.setTextColor(0xFF0000F0);

  190. //          }

  191.            view.setTypeface(Typeface.SANS_SERIF);  

  192.        }  

  193. @Override

  194. public View getItem(int index, View convertView, ViewGroup parent) {  

  195.            currentItem = index;  

  196. returnsuper.getItem(index, convertView, parent);  

  197.        }  

  198.    }  

  199. @Override

  200. protectedvoid onDestroy() {  

  201. if (db != null) {  

  202.            db.close();  

  203.            db = null;  

  204.        }  

  205. super.onDestroy();  

  206.    }  

首先定義了三個數組,分別用來裝查詢到的省,市和地區,其實在這三個之中,省份是不會動態變得。每次都同樣,關鍵就在於,每次省滾輪改變後要查詢到對應的市和市對應的地區數據,要及時查詢出來,否則會有卡頓的感受,流暢性很差。個人數據庫起初是放在assert目錄下面,而後把它寫到應用程序對應的/data/data/包名/files目錄下,而後在該目錄下打開數據庫。這裏涉及到一些緩衝和效率方面的東西,我如今是公用全局的SQLiteDatabase對象db,不是每次都打開數據庫。而後每次查詢都只查詢出對用所須要的列的數據來,這個能夠加快查詢。原本我想實現滾輪選擇某個數據的時候該數據行深色顯示,可是發現好像挺難實現的,android-wheel好像作的不是很強大,也多是我愚笨沒找到實現的方法吧。有知道的麻煩說句啊,謝了。


最後一個類是工具類,包含一些數據庫操做和文件讀寫的。我就不貼出來了。我已經把這個工程放到Github上去了地址https://github.com/ywenblocker/Provinces-Picker-wheel你們能夠看看,有問題也歡迎提出來。最後附上一張效果圖。

20130920234642500

相關文章
相關標籤/搜索