1. 程式人生 > 程式設計 >Android實現城市選擇三級聯動

Android實現城市選擇三級聯動

本文例項為大家分享了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,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,"REGION_NAME":"瀋河區"},{"ID":7975,"REGION_NAME":"大東區"},{"ID":7976,"REGION_NAME":"皇姑區"},{"ID":7977,"REGION_NAME":"鐵西區"},{"ID":7978,"REGION_NAME":"甦家屯區"},{"ID":7979,"REGION_NAME":"東陵區"},{"ID":7980,"REGION_NAME":"新城子區"},{"ID":7981,"REGION_NAME":"于洪區"},{"ID":7982,"REGION_NAME":"遼中縣"},{"ID":7983,"REGION_NAME":"康平縣"},{"ID":7984,"REGION_NAME":"法庫縣"},{"ID":7985,"REGION_NAME":"新民市"},{"ID":7986,"REGION_NAME":"渾南新區"},{"ID":7987,"REGION_NAME":"張士開發區"},{"ID":7988,"REGION_NAME":"沈北新區"},{"ID":7989,"REGION_NAME":"其它區"}]},{"ID":7990,"REGION_NAME":"大連市","res":[{"ID":7991,"PARENT_ID":7990,"REGION_NAME":"中山區"},{"ID":7992,"REGION_NAME":"西崗區"},{"ID":7993,"REGION_NAME":"沙河口區"},{"ID":7994,"REGION_NAME":"甘井子區"},{"ID":7995,"REGION_NAME":"旅順口區"},{"ID":7996,"REGION_NAME":"金州區"},{"ID":7997,"REGION_NAME":"長海縣"},{"ID":7998,"REGION_NAME":"開發區"},{"ID":7999,"REGION_NAME":"瓦房店市"},{"ID":8000,"REGION_NAME":"普蘭店市"},{"ID":8001,"REGION_NAME":"莊河市"},{"ID":8002,"REGION_NAME":"嶺前區"},{"ID":8003,{"ID":8004,"REGION_NAME":"鞍山市","res":[{"ID":8005,"PARENT_ID":8004,"REGION_NAME":"鐵東區"},{"ID":8006,{"ID":8007,"REGION_NAME":"立山區"},{"ID":8008,"REGION_NAME":"千山區"},{"ID":8009,"REGION_NAME":"檯安縣"},{"ID":8010,"REGION_NAME":"岫巖滿族自治縣"},{"ID":8011,"REGION_NAME":"高新區"},{"ID":8012,"REGION_NAME":"海城市"},{"ID":8013,{"ID":8014,"REGION_NAME":"撫順市","res":[{"ID":8015,"PARENT_ID":8014,"REGION_NAME":"新撫區"},{"ID":8016,"REGION_NAME":"東洲區"},{"ID":8017,"REGION_NAME":"望花區"},{"ID":8018,"REGION_NAME":"順城區"},{"ID":8019,"REGION_NAME":"撫順縣"},{"ID":8020,"REGION_NAME":"新賓滿族自治縣"},{"ID":8021,"REGION_NAME":"清原滿族自治縣"},{"ID":8022,{"ID":8023,"REGION_NAME":"本溪市","res":[{"ID":8024,"PARENT_ID":8023,"REGION_NAME":"平山區"},{"ID":8025,"REGION_NAME":"溪湖區"},{"ID":8026,"REGION_NAME":"明山區"},{"ID":8027,"REGION_NAME":"南芬區"},{"ID":8028,"REGION_NAME":"本溪滿族自治縣"},{"ID":8029,"REGION_NAME":"桓仁滿族自治縣"},{"ID":8030,{"ID":8031,"REGION_NAME":"丹東市","res":[{"ID":8032,"PARENT_ID":8031,"REGION_NAME":"元寶區"},{"ID":8033,"REGION_NAME":"振興區"},{"ID":8034,"REGION_NAME":"振安區"},{"ID":8035,"REGION_NAME":"寬甸滿族自治縣"},{"ID":8036,"REGION_NAME":"東港市"},{"ID":8037,"REGION_NAME":"鳳城市"},{"ID":8038,{"ID":8039,"REGION_NAME":"錦州市","res":[{"ID":8040,"PARENT_ID":8039,"REGION_NAME":"古塔區"},{"ID":8041,"REGION_NAME":"淩河區"},{"ID":8042,"REGION_NAME":"太和區"},{"ID":8043,"REGION_NAME":"黑山縣"},{"ID":8044,"REGION_NAME":"義縣"},{"ID":8045,"REGION_NAME":"凌海市"},{"ID":8046,"REGION_NAME":"北鎮市"},{"ID":8047,{"ID":8048,"REGION_NAME":"營口市","res":[{"ID":8049,"PARENT_ID":8048,"REGION_NAME":"站前區"},{"ID":8050,"REGION_NAME":"西市區"},{"ID":8051,"REGION_NAME":"鮁魚圈區"},{"ID":8052,"REGION_NAME":"老邊區"},{"ID":8053,"REGION_NAME":"蓋州市"},{"ID":8054,"REGION_NAME":"大石橋市"},{"ID":8055,{"ID":8056,"REGION_NAME":"阜新市","res":[{"ID":8057,"PARENT_ID":8056,"REGION_NAME":"海州區"},{"ID":8058,"REGION_NAME":"新邱區"},{"ID":8059,"REGION_NAME":"太平區"},{"ID":8060,"REGION_NAME":"清河門區"},{"ID":8061,"REGION_NAME":"細河區"},{"ID":8062,"REGION_NAME":"阜新蒙古族自治縣"},{"ID":8063,"REGION_NAME":"彰武縣"},{"ID":8064,{"ID":8065,"REGION_NAME":"遼陽市","res":[{"ID":8066,"PARENT_ID":8065,"REGION_NAME":"白塔區"},{"ID":8067,"REGION_NAME":"文聖區"},{"ID":8068,"REGION_NAME":"巨集偉區"},{"ID":8069,"REGION_NAME":"弓長嶺區"},{"ID":8070,"REGION_NAME":"太子河區"},{"ID":8071,"REGION_NAME":"遼陽縣"},{"ID":8072,"REGION_NAME":"燈塔市"},{"ID":8073,{"ID":8074,"REGION_NAME":"盤錦市","res":[{"ID":8075,"PARENT_ID":8074,"REGION_NAME":"雙臺子區"},{"ID":8076,"REGION_NAME":"興隆臺區"},{"ID":8077,"REGION_NAME":"大窪縣"},{"ID":8078,"REGION_NAME":"盤山縣"},{"ID":8079,{"ID":8080,"REGION_NAME":"鐵嶺市","res":[{"ID":8081,"PARENT_ID":8080,"REGION_NAME":"銀州區"},{"ID":8082,"REGION_NAME":"清河區"},{"ID":8083,"REGION_NAME":"鐵嶺縣"},{"ID":8084,"REGION_NAME":"西豐縣"},{"ID":8085,"REGION_NAME":"昌圖縣"},{"ID":8086,"REGION_NAME":"調兵山市"},{"ID":8087,"REGION_NAME":"開原市"},{"ID":8088,{"ID":8089,"REGION_NAME":"朝陽市","res":[{"ID":8090,"PARENT_ID":8089,"REGION_NAME":"雙塔區"},{"ID":8091,"REGION_NAME":"龍城區"},{"ID":8092,"REGION_NAME":"朝陽縣"},{"ID":8093,"REGION_NAME":"建平縣"},{"ID":8094,"REGION_NAME":"喀喇沁左翼蒙古族自治縣"},{"ID":8095,"REGION_NAME":"北票市"},{"ID":8096,"REGION_NAME":"凌源市"},{"ID":8097,{"ID":8098,"REGION_NAME":"葫蘆島市","res":[{"ID":8099,"PARENT_ID":8098,"REGION_NAME":"連山區"},{"ID":8100,"REGION_NAME":"龍港區"},{"ID":8101,"REGION_NAME":"南票區"},{"ID":8102,"REGION_NAME":"綏中縣"},{"ID":8103,"REGION_NAME":"建昌縣"},{"ID":8104,"REGION_NAME":"興城市"},{"ID":8105,"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,"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地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。