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

发布时间:2020-11-24 16:36:42 作者:Leah
来源:亿速云 阅读:383

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

popupwindow中是三个wheelview,

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="#00000000"
 android:gravity="bottom"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <LinearLayout
 android:id="@+id/ly_myinfo_changeaddress_child"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:background="#ffffff"
 android:orientation="vertical" >


 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="44dp"
 >

 <View
 android:background="@color/silver"
 android:layout_width="match_parent"
 android:layout_height="0.5dp" />

 <TextView
 android:id="@+id/btn_myinfo_cancel"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:paddingLeft="18dp"
 android:text="取消"
 android:gravity="center"
 android:layout_alignParentLeft="true"
 android:layout_marginRight="15dip"
 android:textColor="#e84515"
 android:textSize="14sp" />


 <TextView
 android:id="@+id/btn_myinfo_sure"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_alignParentRight="true"
 android:gravity="center"
 android:text="完成"
 android:textColor="#e84515"
 android:paddingRight="18dp"
 android:textSize="14sp" />

 </RelativeLayout>

 <View android:layout_width="match_parent"
 android:layout_height="1dp"
 android:background="#d8d8d8"/>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="190dip"
 android:orientation="horizontal"
 android:gravity="center_vertical">

 <guozhaohui.com.wlylocationchoose.locationchoose.WheelView
 android:id="@+id/provinceView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"/>


 <guozhaohui.com.wlylocationchoose.locationchoose.WheelView
 android:id="@+id/cityView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"/>
 <guozhaohui.com.wlylocationchoose.locationchoose.WheelView
 android:id="@+id/districtView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"/>
 </LinearLayout>

 </LinearLayout>

</LinearLayout>

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<ProvinceModel> getProvice(SQLiteDatabase db){
 String sql="SELECT * FROM ChooseCityModel where level = 1 ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,null);
 List<ProvinceModel> list=new ArrayList<ProvinceModel>();
 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<CityModel> getCityByParentId(SQLiteDatabase db, String code){
 String sql="SELECT * FROM ChooseCityModel WHERE level = 2 and parentId = &#63; ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,new String[][code]);
 List<CityModel> list=new ArrayList<CityModel>();

 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<DistrictModel> getDistrictById(SQLiteDatabase db, String code){
 //注意这里的parentId其实就是上一级的cityID
 String sql="SELECT * FROM ChooseCityModel WHERE level = 3 and parentId = &#63; ORDER BY cityID";
 Cursor cursor = db.rawQuery(sql,new String[][code]);
 List<DistrictModel> list=new ArrayList<DistrictModel>();
 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实现三级联动就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 如如何使用journalctl命令?
  2. 如使用JavaScript实现抖音罗盘时钟

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android wheelview 三级联动

上一篇:在java项目中怎么设计双链表

下一篇:Android项目中如何利用Intent传递数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》