欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现城市选择三级联动

程序员文章站 2022-06-28 16:06:58
本文实例为大家分享了android实现城市选择三级联动的具体代码,供大家参考,具体内容如下效果图,用于城市选择三级联动,带id返回1. 添加依赖//三级联动 implementation 'com.c...

本文实例为大家分享了android实现城市选择三级联动的具体代码,供大家参考,具体内容如下

效果图,用于城市选择三级联动,带id返回

Android实现城市选择三级联动

1. 添加依赖 

//三级联动
 implementation 'com.contrarywind:android-pickerview:4.1.8'
 // gosn解析
 implementation 'com.google.code.gson:gson:2.8.5'

2.文件转换成json串工具类

import android.content.context;
import android.content.res.assetmanager;
 
import java.io.bufferedinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;
 
/**
 * created by dell on 2019/9/16.
 */
 
public class jsonfilereader {
 public static string getjson(context context, string filename) {
 bytearrayoutputstream baos = new bytearrayoutputstream();
 try {
  assetmanager assetmanager = context.getassets();
  inputstream inputstream = assetmanager.open(filename);
  bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream);
  byte[] buffer = new byte[1024];
  int len;
  while ((len = bufferedinputstream.read(buffer)) != -1) {
  baos.write(buffer, 0, len);
  }
 } catch (ioexception e) {
  e.printstacktrace();
 }
 return baos.tostring();
 }
}

3.json转换成集合工具类

import android.content.context;
 
import com.google.gson.gson;
 
import org.json.jsonarray;
 
import java.util.arraylist;
 
/**
 * created by dell on 2019/9/16.
 */
 
public class levelslistdate {
 private arraylist<jsonbean> options1items = new arraylist<>();
 private arraylist<arraylist<string>> options2items = new arraylist<>();
 private arraylist<arraylist<arraylist<string>>> options3items = new arraylist<>();
 private context context;
 
 public levelslistdate(context context) {
 this.context = context;
 }
 
 public arraylist<jsonbean> initjsondata(string path) {
 string jsondata = jsonfilereader.getjson(context, path);
 options1items.clear();
 options1items = parsedata(jsondata);//用gson 转成实体
 return options1items;
 }
 
 public arraylist<arraylist<string>> initjsondata1(string path) {
 string jsondata = jsonfilereader.getjson(context, path);
 arraylist<jsonbean> jsonbean = parsedata(jsondata);//用gson 转成实体
 options2items.clear();
 for (int i = 0; i < jsonbean.size(); i++) {//遍历省份
  arraylist<string> citylist = new arraylist<>();//该省的城市列表(第二级)
  arraylist<arraylist<string>> province_arealist = new arraylist<>();//该省的所有地区列表(第三极)
  for (int c = 0; c < jsonbean.get(i).getcity().size(); c++) {//遍历该省份的所有城市
  string cityname = jsonbean.get(i).getcity().get(c).getregion_name();
  citylist.add(cityname);//添加城市
  arraylist<string> city_arealist = new arraylist<>();//该城市的所有地区列表
  //如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃
  if (jsonbean.get(i).getcity().get(c).getres() == null
   || jsonbean.get(i).getcity().get(c).getres().size() == 0) {
   city_arealist.add("");
  } else {
   for (int d = 0; d < jsonbean.get(i).getcity().get(c).getres().size(); d++) {//该城市对应地区所有数据
   string areaname = jsonbean.get(i).getcity().get(c).getres().get(d).getregion_name();
   city_arealist.add(areaname);//添加该城市所有地区数据
   }
  }
  province_arealist.add(city_arealist);//添加该省所有地区数据
  }
  /**
  * 添加城市数据
  */
  options2items.add(citylist);
 }
 return options2items;
 }
 
 public arraylist<arraylist<arraylist<string>>> initjsondata2(string path) {
 string jsondata = jsonfilereader.getjson(context, path);
 arraylist<jsonbean> jsonbean = parsedata(jsondata);//用gson 转成实体
 options3items.clear();
 for (int i = 0; i < jsonbean.size(); i++) {//遍历省份
  arraylist<string> citylist = new arraylist<>();//该省的城市列表(第二级)
  arraylist<arraylist<string>> province_arealist = new arraylist<>();//该省的所有地区列表(第三极)
  for (int c = 0; c < jsonbean.get(i).getcity().size(); c++) {//遍历该省份的所有城市
  string cityname = jsonbean.get(i).getcity().get(c).getregion_name();
  citylist.add(cityname);//添加城市
  arraylist<string> city_arealist = new arraylist<>();//该城市的所有地区列表
  //如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃
  if (jsonbean.get(i).getcity().get(c).getres() == null
   || jsonbean.get(i).getcity().get(c).getres().size() == 0) {
   city_arealist.add("");
  } else {
   for (int d = 0; d < jsonbean.get(i).getcity().get(c).getres().size(); d++) {//该城市对应地区所有数据
   string areaname = jsonbean.get(i).getcity().get(c).getres().get(d).getregion_name();
   city_arealist.add(areaname);//添加该城市所有地区数据
   }
  }
  province_arealist.add(city_arealist);//添加该省所有地区数据
  }
  /**
  * 添加地区数据
  */
  options3items.add(province_arealist);
 }
 return options3items;
 }
 
 public arraylist<jsonbean> parsedata(string result) {//gson 解析
 arraylist<jsonbean> detail = new arraylist<>();
 try {
  jsonarray data = new jsonarray(result);
  gson gson = new gson();
  for (int i = 0; i < data.length(); i++) {
  jsonbean entity = gson.fromjson(data.optjsonobject(i).tostring(), jsonbean.class);
  detail.add(entity);
  }
 } catch (exception e) {
  e.printstacktrace();
 }
 return detail;
 }
}

4.jsonbean类

import com.contrarywind.interfaces.ipickerviewdata;
 
import java.util.list;
 
/**
 * created by dell on 2019/9/16.
 */
 
public class jsonbean implements ipickerviewdata {
 
 /**
 * id : 7971
 * parent_id : 7459
 * region_name : 辽宁省
 * city : [{"id":7972,"parent_id":7971,"region_name":"沈阳市","res":[{"id":7973,"parent_id":7972,"region_name":"和平区"},{"id":7974,"parent_id":7972,"region_name":"沈河区"},{"id":7975,"parent_id":7972,"region_name":"大东区"},{"id":7976,"parent_id":7972,"region_name":"皇姑区"},{"id":7977,"parent_id":7972,"region_name":"铁西区"},{"id":7978,"parent_id":7972,"region_name":"苏家屯区"},{"id":7979,"parent_id":7972,"region_name":"东陵区"},{"id":7980,"parent_id":7972,"region_name":"新城子区"},{"id":7981,"parent_id":7972,"region_name":"于洪区"},{"id":7982,"parent_id":7972,"region_name":"辽中县"},{"id":7983,"parent_id":7972,"region_name":"康平县"},{"id":7984,"parent_id":7972,"region_name":"法库县"},{"id":7985,"parent_id":7972,"region_name":"新民市"},{"id":7986,"parent_id":7972,"region_name":"浑南新区"},{"id":7987,"parent_id":7972,"region_name":"张士开发区"},{"id":7988,"parent_id":7972,"region_name":"沈北新区"},{"id":7989,"parent_id":7972,"region_name":"其它区"}]},{"id":7990,"parent_id":7971,"region_name":"大连市","res":[{"id":7991,"parent_id":7990,"region_name":"中山区"},{"id":7992,"parent_id":7990,"region_name":"西岗区"},{"id":7993,"parent_id":7990,"region_name":"沙河口区"},{"id":7994,"parent_id":7990,"region_name":"甘井子区"},{"id":7995,"parent_id":7990,"region_name":"旅顺口区"},{"id":7996,"parent_id":7990,"region_name":"金州区"},{"id":7997,"parent_id":7990,"region_name":"长海县"},{"id":7998,"parent_id":7990,"region_name":"开发区"},{"id":7999,"parent_id":7990,"region_name":"瓦房店市"},{"id":8000,"parent_id":7990,"region_name":"普兰店市"},{"id":8001,"parent_id":7990,"region_name":"庄河市"},{"id":8002,"parent_id":7990,"region_name":"岭前区"},{"id":8003,"parent_id":7990,"region_name":"其它区"}]},{"id":8004,"parent_id":7971,"region_name":"鞍山市","res":[{"id":8005,"parent_id":8004,"region_name":"铁东区"},{"id":8006,"parent_id":8004,"region_name":"铁西区"},{"id":8007,"parent_id":8004,"region_name":"立山区"},{"id":8008,"parent_id":8004,"region_name":"千山区"},{"id":8009,"parent_id":8004,"region_name":"台安县"},{"id":8010,"parent_id":8004,"region_name":"岫岩满族自治县"},{"id":8011,"parent_id":8004,"region_name":"高新区"},{"id":8012,"parent_id":8004,"region_name":"海城市"},{"id":8013,"parent_id":8004,"region_name":"其它区"}]},{"id":8014,"parent_id":7971,"region_name":"抚顺市","res":[{"id":8015,"parent_id":8014,"region_name":"新抚区"},{"id":8016,"parent_id":8014,"region_name":"东洲区"},{"id":8017,"parent_id":8014,"region_name":"望花区"},{"id":8018,"parent_id":8014,"region_name":"顺城区"},{"id":8019,"parent_id":8014,"region_name":"抚顺县"},{"id":8020,"parent_id":8014,"region_name":"新宾满族自治县"},{"id":8021,"parent_id":8014,"region_name":"清原满族自治县"},{"id":8022,"parent_id":8014,"region_name":"其它区"}]},{"id":8023,"parent_id":7971,"region_name":"本溪市","res":[{"id":8024,"parent_id":8023,"region_name":"平山区"},{"id":8025,"parent_id":8023,"region_name":"溪湖区"},{"id":8026,"parent_id":8023,"region_name":"明山区"},{"id":8027,"parent_id":8023,"region_name":"南芬区"},{"id":8028,"parent_id":8023,"region_name":"本溪满族自治县"},{"id":8029,"parent_id":8023,"region_name":"桓仁满族自治县"},{"id":8030,"parent_id":8023,"region_name":"其它区"}]},{"id":8031,"parent_id":7971,"region_name":"丹东市","res":[{"id":8032,"parent_id":8031,"region_name":"元宝区"},{"id":8033,"parent_id":8031,"region_name":"振兴区"},{"id":8034,"parent_id":8031,"region_name":"振安区"},{"id":8035,"parent_id":8031,"region_name":"宽甸满族自治县"},{"id":8036,"parent_id":8031,"region_name":"东港市"},{"id":8037,"parent_id":8031,"region_name":"凤城市"},{"id":8038,"parent_id":8031,"region_name":"其它区"}]},{"id":8039,"parent_id":7971,"region_name":"锦州市","res":[{"id":8040,"parent_id":8039,"region_name":"古塔区"},{"id":8041,"parent_id":8039,"region_name":"凌河区"},{"id":8042,"parent_id":8039,"region_name":"太和区"},{"id":8043,"parent_id":8039,"region_name":"黑山县"},{"id":8044,"parent_id":8039,"region_name":"义县"},{"id":8045,"parent_id":8039,"region_name":"凌海市"},{"id":8046,"parent_id":8039,"region_name":"北镇市"},{"id":8047,"parent_id":8039,"region_name":"其它区"}]},{"id":8048,"parent_id":7971,"region_name":"营口市","res":[{"id":8049,"parent_id":8048,"region_name":"站前区"},{"id":8050,"parent_id":8048,"region_name":"西市区"},{"id":8051,"parent_id":8048,"region_name":"鲅鱼圈区"},{"id":8052,"parent_id":8048,"region_name":"老边区"},{"id":8053,"parent_id":8048,"region_name":"盖州市"},{"id":8054,"parent_id":8048,"region_name":"大石桥市"},{"id":8055,"parent_id":8048,"region_name":"其它区"}]},{"id":8056,"parent_id":7971,"region_name":"阜新市","res":[{"id":8057,"parent_id":8056,"region_name":"海州区"},{"id":8058,"parent_id":8056,"region_name":"新邱区"},{"id":8059,"parent_id":8056,"region_name":"太平区"},{"id":8060,"parent_id":8056,"region_name":"清河门区"},{"id":8061,"parent_id":8056,"region_name":"细河区"},{"id":8062,"parent_id":8056,"region_name":"阜新蒙古族自治县"},{"id":8063,"parent_id":8056,"region_name":"彰武县"},{"id":8064,"parent_id":8056,"region_name":"其它区"}]},{"id":8065,"parent_id":7971,"region_name":"辽阳市","res":[{"id":8066,"parent_id":8065,"region_name":"白塔区"},{"id":8067,"parent_id":8065,"region_name":"文圣区"},{"id":8068,"parent_id":8065,"region_name":"宏伟区"},{"id":8069,"parent_id":8065,"region_name":"弓长岭区"},{"id":8070,"parent_id":8065,"region_name":"太子河区"},{"id":8071,"parent_id":8065,"region_name":"辽阳县"},{"id":8072,"parent_id":8065,"region_name":"灯塔市"},{"id":8073,"parent_id":8065,"region_name":"其它区"}]},{"id":8074,"parent_id":7971,"region_name":"盘锦市","res":[{"id":8075,"parent_id":8074,"region_name":"双台子区"},{"id":8076,"parent_id":8074,"region_name":"兴隆台区"},{"id":8077,"parent_id":8074,"region_name":"大洼县"},{"id":8078,"parent_id":8074,"region_name":"盘山县"},{"id":8079,"parent_id":8074,"region_name":"其它区"}]},{"id":8080,"parent_id":7971,"region_name":"铁岭市","res":[{"id":8081,"parent_id":8080,"region_name":"银州区"},{"id":8082,"parent_id":8080,"region_name":"清河区"},{"id":8083,"parent_id":8080,"region_name":"铁岭县"},{"id":8084,"parent_id":8080,"region_name":"西丰县"},{"id":8085,"parent_id":8080,"region_name":"昌图县"},{"id":8086,"parent_id":8080,"region_name":"调兵山市"},{"id":8087,"parent_id":8080,"region_name":"开原市"},{"id":8088,"parent_id":8080,"region_name":"其它区"}]},{"id":8089,"parent_id":7971,"region_name":"朝阳市","res":[{"id":8090,"parent_id":8089,"region_name":"双塔区"},{"id":8091,"parent_id":8089,"region_name":"龙城区"},{"id":8092,"parent_id":8089,"region_name":"朝阳县"},{"id":8093,"parent_id":8089,"region_name":"建平县"},{"id":8094,"parent_id":8089,"region_name":"喀喇沁左翼蒙古族自治县"},{"id":8095,"parent_id":8089,"region_name":"北票市"},{"id":8096,"parent_id":8089,"region_name":"凌源市"},{"id":8097,"parent_id":8089,"region_name":"其它区"}]},{"id":8098,"parent_id":7971,"region_name":"葫芦岛市","res":[{"id":8099,"parent_id":8098,"region_name":"连山区"},{"id":8100,"parent_id":8098,"region_name":"龙港区"},{"id":8101,"parent_id":8098,"region_name":"南票区"},{"id":8102,"parent_id":8098,"region_name":"绥中县"},{"id":8103,"parent_id":8098,"region_name":"建昌县"},{"id":8104,"parent_id":8098,"region_name":"兴城市"},{"id":8105,"parent_id":8098,"region_name":"其它区"}]}]
 */
 
 private int id;
 private int parent_id;
 private string region_name;
 private list<citybean> city;
 
 public int getid() {
 return id;
 }
 
 public void setid(int id) {
 this.id = id;
 }
 
 public int getparent_id() {
 return parent_id;
 }
 
 public void setparent_id(int parent_id) {
 this.parent_id = parent_id;
 }
 
 public string getregion_name() {
 return region_name;
 }
 
 public void setregion_name(string region_name) {
 this.region_name = region_name;
 }
 
 public list<citybean> getcity() {
 return city;
 }
 
 public void setcity(list<citybean> city) {
 this.city = city;
 }
 
 @override
 public string getpickerviewtext() {
 return this.region_name;
 }
 
 public static class citybean {
 /**
  * id : 7972
  * parent_id : 7971
  * region_name : 沈阳市
  * res : [{"id":7973,"parent_id":7972,"region_name":"和平区"},{"id":7974,"parent_id":7972,"region_name":"沈河区"},{"id":7975,"parent_id":7972,"region_name":"大东区"},{"id":7976,"parent_id":7972,"region_name":"皇姑区"},{"id":7977,"parent_id":7972,"region_name":"铁西区"},{"id":7978,"parent_id":7972,"region_name":"苏家屯区"},{"id":7979,"parent_id":7972,"region_name":"东陵区"},{"id":7980,"parent_id":7972,"region_name":"新城子区"},{"id":7981,"parent_id":7972,"region_name":"于洪区"},{"id":7982,"parent_id":7972,"region_name":"辽中县"},{"id":7983,"parent_id":7972,"region_name":"康平县"},{"id":7984,"parent_id":7972,"region_name":"法库县"},{"id":7985,"parent_id":7972,"region_name":"新民市"},{"id":7986,"parent_id":7972,"region_name":"浑南新区"},{"id":7987,"parent_id":7972,"region_name":"张士开发区"},{"id":7988,"parent_id":7972,"region_name":"沈北新区"},{"id":7989,"parent_id":7972,"region_name":"其它区"}]
  */
 
 private int id;
 private int parent_id;
 private string region_name;
 private list<resbean> res;
 
 public int getid() {
  return id;
 }
 
 public void setid(int id) {
  this.id = id;
 }
 
 public int getparent_id() {
  return parent_id;
 }
 
 public void setparent_id(int parent_id) {
  this.parent_id = parent_id;
 }
 
 public string getregion_name() {
  return region_name;
 }
 
 public void setregion_name(string region_name) {
  this.region_name = region_name;
 }
 
 public list<resbean> getres() {
  return res;
 }
 
 public void setres(list<resbean> res) {
  this.res = res;
 }
 
 public static class resbean {
  /**
  * id : 7973
  * parent_id : 7972
  * region_name : 和平区
  */
 
  private int id;
  private int parent_id;
  private string region_name;
 
  public int getid() {
  return id;
  }
 
  public void setid(int id) {
  this.id = id;
  }
 
  public int getparent_id() {
  return parent_id;
  }
 
  public void setparent_id(int parent_id) {
  this.parent_id = parent_id;
  }
 
  public string getregion_name() {
  return region_name;
  }
 
  public void setregion_name(string region_name) {
  this.region_name = region_name;
  }
 }
 }
}

5.主页调用,城市json数据很多解析耗时,进入页面会非常的慢,所以在子线程调用。有的地址没有第三级trycach,有就取没有就去二级

import android.os.handler;
import android.os.message;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.textview;
import com.bigkoo.pickerview.builder.optionspickerbuilder;
import com.bigkoo.pickerview.view.optionspickerview;
 
import java.util.arraylist;
 
 
public class mainactivity extends appcompatactivity {
 private textview tvaddress;
 private optionspickerview pvoptions;
 private levelslistdate levelslistdate;
 private arraylist<jsonbean> jsonbeans;
 private arraylist<arraylist<string>> arraylists;
 private arraylist<arraylist<arraylist<string>>> arraylists1;
 private handler handler1 = new handler() {
 @override
 public void handlemessage(message msg) {
  super.handlemessage(msg);
  try {
  showhypickerview();
  } catch (exception e) {
  e.printstacktrace();
  }
 }
 };
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 tvaddress = findviewbyid(r.id.tvaddress);
 tvaddress.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if (pvoptions != null) {
   pvoptions.show();
  }
  }
 });
 new thread(new runnable() {
  @override
  public void run() {
  try {
   levelslistdate = new levelslistdate(mainactivity.this);
   jsonbeans = levelslistdate.initjsondata("citys_data.json");
   arraylists = levelslistdate.initjsondata1("citys_data.json");
   arraylists1 = levelslistdate.initjsondata2("citys_data.json");
   handler1.sendemptymessage(1);
  } catch (exception e) {
   e.printstacktrace();
  }
  }
 }).start();
 }
 
 /**
 * 初始化城市选择器
 */
 private void showhypickerview() {
 //条件选择器
 pvoptions = new optionspickerbuilder(mainactivity.this, new com.bigkoo.pickerview.listener.onoptionsselectlistener() {
  @override
  public void onoptionsselect(int options1, int options2, int options3, view v) {
  try {
   // cityid = jsonbeans.get(options1).getcity().get(options2).getres().get(options3).getid() + "";
   tvaddress.settext(jsonbeans.get(options1).getcity().get(options2).getres().get(options3).getregion_name());
  } catch (exception e) {
   // cityid = jsonbeans.get(options1).getcity().get(options2).getid() + "";
   tvaddress.settext(jsonbeans.get(options1).getcity().get(options2).getregion_name());
  }
  }
 }).build();
 pvoptions.setpicker(jsonbeans, arraylists, arraylists1);
 }
}

github地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。