如何在Android中使用WheelView实现三级联动

这篇文章将为大家详细讲解有关如何在Android中使用WheelView实现三级联动,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

10年积累的做网站、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有费县免费网站建设让你可以放心的选择与我们合作。

popupwindow中是三个wheelview,

<?xml version="1.0" encoding="utf-8"?>


 


 

 

 


 

 

 

 

 


 
 
 

 

b. 因为上面说了,需要将文件copy到app目录下,所以直接最好这代码在application中写,

package guozhaohui.com.wlylocationchoose;

import android.app.Application;

import java.io.InputStream;

import guozhaohui.com.wlylocationchoose.locationchoose.CityDataHelper;

/**
 * Created by ${GuoZhaoHui} on 2017/2/13.
 * Abstract:
 */

public class MyApplication extends Application {

 private CityDataHelper dataHelper;

 @Override
 public void onCreate() {
 super.onCreate();

 /**
 * 放在application中,让app一启动就把raw中文件copy到 "/data/data/"+context.getPackageName()+"/databases/"
 * 这是app读取数据的方法,不管是将数据库文件放在raw或者assets中都是一样
 */
 dataHelper=CityDataHelper.getInstance(this);
 InputStream in = this.getResources().openRawResource(R.raw.city);
 dataHelper.copyFile(in,CityDataHelper.DATABASE_NAME,CityDataHelper.DATABASES_DIR);

 }
}

c. popupwindow不是本次的重点也直接贴代码。

 View popupView = LayoutInflater.from(this).inflate(R.layout.popup_locationchoose, null);
 mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
 mPopupWindow.setTouchable(true);
 mPopupWindow.setFocusable(true);
 mPopupWindow.setOutsideTouchable(true);
 mPopupWindow.setAnimationStyle(R.style.popup_locationchoose_bottom);

 // pickText = (TextView)popupView.findViewById(R.id.tv_pickText);
 provinceView = (WheelView)popupView.findViewById(R.id.provinceView);
 cityView = (WheelView)popupView.findViewById(R.id.cityView);
 districtView = (WheelView)popupView.findViewById(R.id.districtView);

 //确定或者取消
 btn_myinfo_sure = (TextView)popupView.findViewById(R.id.btn_myinfo_sure);
 btn_myinfo_cancel = (TextView) popupView.findViewById(R.id.btn_myinfo_cancel);
 btn_myinfo_cancel.setOnClickListener(this);
 btn_myinfo_sure.setOnClickListener(this);

设置三个wheelview的可见条目数

provinceView.setVisibleItems(7);
cityView.setVisibleItems(7);
districtView.setVisibleItems(7);

为三个 wheelview添加滑动事件

 // 添加change事件
 provinceView.addChangingListener(this);
 // 添加change事件
 cityView.addChangingListener(this);
 // 添加change事件
 districtView.addChangingListener(this);

c. 拿到操作数据的对象SQLiteDatabase

 db = dataHelper.openDataBase();

观察数据库文件可知这表中是根据字段level来判断省市区的,如图

如何在Android中使用WheelView实现三级联动

同时我们也可知这省市区三个模型中的字段都是一样的,都是

 private int cityID;
 private int parentId;
 private int level;
 private String name;
 private String pinyin;

不清楚的可以自己查下表,如图,我查一个省

如何在Android中使用WheelView实现三级联动

所以我们建立一样的模型,虽然三个字段是一样的,建一个就可以了,但是为了标准最好还是建三个。

package guozhaohui.com.wlylocationchoose.locationchoose.model;

public class ProvinceModel {

 private int cityID;
 private int parentId;
 private int level;
 private String name;
 private String pinyin;

 public int getCityID() {
 return cityID;
 }

 public void setCityID(int cityID) {
 this.cityID = cityID;
 }

 public int getParentId() {
 return parentId;
 }

 public void setParentId(int parentId) {
 this.parentId = parentId;
 }

 public int getLevel() {
 return level;
 }

 public void setLevel(int level) {
 this.level = level;
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getPinyin() {
 return pinyin;
 }

 public void setPinyin(String pinyin) {
 this.pinyin = pinyin;
 }


}

进行sql查询,将查询到的结果保存在cursor中,然后进行一行行的循环遍历,然后将遍历一行的对象添加到这个对象的集合中。这里得到省的集合。

 public List getProvice(SQLiteDatabase db){
 String sql="SELECT * FROM ChooseCityModel where level = 1 ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,null);
 List list=new ArrayList();
 if (cursor!=null&&cursor.getCount() > 0) {
 while (cursor.moveToNext()){
 ProvinceModel provinceModel=new ProvinceModel();
 provinceModel.setCityID(cursor.getInt(cursor.getColumnIndex("cityID")));
 provinceModel.setParentId(cursor.getInt(cursor.getColumnIndex("parentId")));
 provinceModel.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
 provinceModel.setName(cursor.getString(cursor.getColumnIndex("name")));
 provinceModel.setPinyin(cursor.getString(cursor.getColumnIndex("pinyin")));
 list.add(provinceModel);
 }
 }
 return list;
 }

根据表的结构,得到相应的sql语句,希望根据上一级省的cityId得到下面的市,其实换句话说本级市的parentId就是上一级的cityid,不清楚的可以将sql语句查一遍,验证下对不对,如图

如何在Android中使用WheelView实现三级联动

得到相应省下面市的集合

 public List getCityByParentId(SQLiteDatabase db, String code){
 String sql="SELECT * FROM ChooseCityModel WHERE level = 2 and parentId = ? ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,new String[][code]);
 List list=new ArrayList();

 if (cursor!=null&&cursor.getCount() > 0) {

 while (cursor.moveToNext()){
 CityModel cityModel=new CityModel();
 cityModel.setCityID(cursor.getInt(cursor.getColumnIndex("cityID")));
 cityModel.setParentId(cursor.getInt(cursor.getColumnIndex("parentId")));
 cityModel.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
 cityModel.setName(cursor.getString(cursor.getColumnIndex("name")));
 cityModel.setPinyin(cursor.getString(cursor.getColumnIndex("pinyin")));
 list.add(cityModel);
 }
 }
 return list;
 }

区也是一样的,直接贴代码了

 public List getDistrictById(SQLiteDatabase db, String code){
 //注意这里的parentId其实就是上一级的cityID
 String sql="SELECT * FROM ChooseCityModel WHERE level = 3 and parentId = ? ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,new String[][code]);
 List list=new ArrayList();
 if (cursor!=null&&cursor.getCount() > 0) {
 while (cursor.moveToNext()){
 DistrictModel districtModel=new DistrictModel();
 districtModel.setCityID(cursor.getInt(cursor.getColumnIndex("cityID")));
 districtModel.setParentId(cursor.getInt(cursor.getColumnIndex("parentId")));
 districtModel.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
 districtModel.setName(cursor.getString(cursor.getColumnIndex("name")));
 districtModel.setPinyin(cursor.getString(cursor.getColumnIndex("pinyin")));
 list.add(districtModel);
 }
 }
 return list;
 }

d. 对弹出popuwindow显示的wheelview进行初始化,注释都写在代码里,

 private void initpopData() {
 //初始化数据
 dataHelper = CityDataHelper.getInstance(this);
 db = dataHelper.openDataBase();
 provinceDatas = dataHelper.getProvice(db);
 if (provinceDatas.size() > 0) {

 //弹出popup时,省wheelview中当前的省其实就是省集合的第一个
 mCurrentProvince = provinceDatas.get(0).getName();

 //根据省cityid查询到第一个省下面市的集合
 cityDatas = dataHelper.getCityByParentId(db, provinceDatas.get(0).getCityID()+"");
 }
 if (cityDatas.size() > 0) {
 //根据市cityid查询到第一个市集合下面区的集合
 districtDatas = dataHelper.getDistrictById(db, cityDatas.get(0).getCityID()+"");

 }
 //wheelview的适配器代码
 provinceAdapter = new ProvinceAdapter(this, provinceDatas);
 provinceAdapter.setTextSize(TEXTSIZE);//设置字体大小
 provinceView.setViewAdapter(provinceAdapter);

 updateCitys();
 updateAreas();
 }

更新省下面市的wheelview内容,注释很清楚,直接上代码

 private void updateCitys() {
 int pCurrent = provinceView.getCurrentItem();
 if (provinceDatas.size() > 0) {
 //这里是必须的的,上面得到的集合只是第一个省下面所有市的集合及第一个市下面所有区的集合
 //这里得到的是相应省下面对应市的集合
 cityDatas = dataHelper.getCityByParentId(db, provinceDatas.get(pCurrent).getCityID()+"");
 } else {
 cityDatas.clear();
 }
 citysAdapter = new CitysAdapter(this, cityDatas);
 citysAdapter.setTextSize(TEXTSIZE);
 cityView.setViewAdapter(citysAdapter);
 if (cityDatas.size() > 0) {
 //默认省下面 市wheelview滑动第一个,显示第一个市
 cityView.setCurrentItem(0);
 mCurrentCity = cityDatas.get(0).getName();
 } else {
 mCurrentCity = "";
 }
 updateAreas();
 }

第三个wheelview和第二个一样的,代码直接上

 private void updateAreas() {
 int cCurrent = cityView.getCurrentItem();
 if (cityDatas.size() > 0) {
 districtDatas = dataHelper.getDistrictById(db, cityDatas.get(cCurrent).getCityID()+"");
 } else {
 districtDatas.clear();
 }
 areaAdapter = new AreaAdapter(this, districtDatas);
 areaAdapter.setTextSize(TEXTSIZE);
 districtView.setViewAdapter(areaAdapter);
 if (districtDatas.size() > 0) {
 mCurrentDistrict = districtDatas.get(0).getName();
 districtView.setCurrentItem(0);
 } else {
 mCurrentDistrict = "";
 }
 }

关于如何在Android中使用WheelView实现三级联动就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前文章:如何在Android中使用WheelView实现三级联动
标题链接:http://pwwzsj.com/article/jghgdc.html