栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现城市选择三级联动

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现城市选择三级联动

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

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

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;
 

 
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;
 

 
public class LevelsListDate {
 private ArrayList options1Items = new ArrayList<>();
 private ArrayList> options2Items = new ArrayList<>();
 private ArrayList>> options3Items = new ArrayList<>();
 private Context context;
 
 public LevelsListDate(Context context) {
 this.context = context;
 }
 
 public ArrayList initJsonData(String path) {
 String JsonData = JsonFileReader.getJson(context, path);
 options1Items.clear();
 options1Items = parseData(JsonData);//用Gson 转成实体
 return options1Items;
 }
 
 public ArrayList> initJsonData1(String path) {
 String JsonData = JsonFileReader.getJson(context, path);
 ArrayList jsonBean = parseData(JsonData);//用Gson 转成实体
 options2Items.clear();
 for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
  ArrayList CityList = new ArrayList<>();//该省的城市列表(第二级)
  ArrayList> 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 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>> initJsonData2(String path) {
 String JsonData = JsonFileReader.getJson(context, path);
 ArrayList jsonBean = parseData(JsonData);//用Gson 转成实体
 options3Items.clear();
 for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
  ArrayList CityList = new ArrayList<>();//该省的城市列表(第二级)
  ArrayList> 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 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 parseData(String result) {//Gson 解析
 ArrayList 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;
 

 
public class JsonBean implements IPickerViewData {
 
 
 
 private int ID;
 private int PARENT_ID;
 private String REGION_NAME;
 private List 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 getCity() {
 return city;
 }
 
 public void setCity(List city) {
 this.city = city;
 }
 
 @Override
 public String getPickerViewText() {
 return this.REGION_NAME;
 }
 
 public static class CityBean {
 
 
 private int ID;
 private int PARENT_ID;
 private String REGION_NAME;
 private List 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 getRes() {
  return res;
 }
 
 public void setRes(List res) {
  this.res = res;
 }
 
 public static class ResBean {
  
 
  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 jsonBeans;
 private ArrayList> arrayLists;
 private ArrayList>> 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地址

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号