最近在github中看到一個很不錯的下拉選擇器專案Android-PickerView,決定自己試一下,
首先,匯入Android-PickerView包:
implementation 'com.contrarywind:Android-PickerView:3.1.0'
然后,去聚合資料申請了一個天氣預報的介面,copy了相關城市資料,然后把它裝到json檔案中,這邊避免了每次都去請求介面呼叫資料,當然,也可以直接在Android中使用sql-lite存盤資料,我的資料部分如下:

然后,我們在Android專案app下的main檔案中創建檔案夾assets檔案夾,把city.json檔案放進去:
接著,我們就可以進行正常撰寫了,首先是撰寫一個CityUtils工具類,主要是為了讀取城市檔案json資料 :
package com.huatec.pickerviewdemo.utils;
import android.content.Context;
import android.content.res.AssetManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huatec.pickerviewdemo.entity.CityBean;
import org.apache.http.util.EncodingUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* 讀取城市json檔案資料工具
* date:2021.06.23
* */
public class CityUtils {
private static final String FILE_NAME="city.json";//檔案名
//讀取json檔案,并轉化為string字串
private static String readJsonFile(Context con){
String s="";
InputStream in=con.getClass().getClassLoader().getResourceAsStream("assets/"+FILE_NAME);
try {
int len=in.available();
byte[] bytes=new byte[len];
in.read(bytes);
s= EncodingUtils.getString(bytes,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
//封裝城市資料,json轉list物體
public static List<CityBean> getCityList(Context con){
String jsonStr=readJsonFile(con);
Gson gson=new Gson();
Type type=new TypeToken<ArrayList<CityBean>>(){}.getType();
List<CityBean> list=gson.fromJson(jsonStr,type);
return list;
}
}
這里,我們用到了CityBean類,主要是為了更好的把json內轉換成object類,所以,我們需要創建CityBean類:
package com.huatec.pickerviewdemo.entity;
//城市物體類
public class CityBean {
private String id;
private String province;
private String city;
private String district;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
@Override
public String toString() {
return "CityBean{" +
"id='" + id + '\'' +
", province='" + province + '\'' +
", city='" + city + '\'' +
", district='" + district + '\'' +
'}';
}
}
然后,我們就可以去使用Android-PickerView了,首先,我們需要對城市資料進行封裝,以滿足選擇器需要:
//部分關鍵代碼
private void cityPicher(){
OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//回傳的分別是三個級別的選中位置
// String tx = op1.get(options1) +"-"+
// op2.get(options1).get(options2);
String chcity="";
if(op3.get(options1).get(options2).size()!=0||op3.get(options1).get(options2)!=null){
// tx+="-";
// tx+=op3.get(options1).get(options2).get(options3);
chcity=op3.get(options1).get(options2).get(options3);;
}else{
chcity=op2.get(options1).get(options2);
}
citypicker.setText(chcity);
}
})
.setTitleText("城市選擇")
.setCancelText("取消")
.setCancelColor(R.style.AppTheme)
.setSubmitText("確定")
.setContentTextSize(20)
.setSelectOptions(25,0,2)
.setOutSideCancelable(false)// default is true
.build();
pvOptions.setPicker(op1, op2, op3);//三級選擇器
pvOptions.show();
}
//封裝資料op1、op2、op3
private void initdata(){
//
List<CityBean> pro=provincelist();
List<CityBean> city=findCitylist();
for (CityBean prolist:pro){
op1.add(prolist.getProvince());
ArrayList<ArrayList<String>> disList=new ArrayList<>();
ArrayList<String> cityList=new ArrayList<>();
for (CityBean cityl:city){
if(prolist.getProvince().equals(cityl.getProvince())){
cityList.add(cityl.getCity());
ArrayList<String> disstr=new ArrayList<>();
for (CityBean cl:citylist){
if (cityl.getCity().equals(cl.getCity())&&(!cl.getDistrict().equals(cl.getCity()))){
disstr.add(cl.getDistrict());
}
}
disList.add(disstr);
}
}
op2.add(cityList);
op3.add(disList);
}
}
//獲取省
private List<CityBean> provincelist(){
List<CityBean> list=new ArrayList<>(citylist);
for (int i=0;i<list.size();i++){
for (int j=i+1;j<list.size();j++){
if(list.get(i).getProvince().equals(list.get(j).getProvince())){
list.remove(j);
j--;
}
}
}
return list;
}
//獲取市
private List<CityBean> findCitylist(){
List<CityBean> list=new ArrayList<>(citylist);
for (int i=0;i<list.size();i++){
for (int j=i+1;j<list.size();j++){
if(list.get(i).getCity().equals(list.get(j).getCity())){
list.remove(j);
j--;
}
}
}
return list;
}
具體效果:

如果對于 Android-PickerView或者代碼有疑問的地方,可以搜索Android-PickerView或者加QQ群:1039603877進行交流,
-------------------------------------------------------------------------
QQ群:1039603877,本群創建于2020/2/3: 本群原為1999人大群,主要是進行java開發技術交流,里面有重慶等地區java開發的業界大佬、牛人,很多群友都是CTO或者技術負責人,歡迎大家進群學習和交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/289625.html
標籤:其他
上一篇:實作一個多語言文案快速轉換工具
