Android PickerView实现 * 联动效果
作者:Knick_Zhang 时间:2023-02-25 15:05:47
GitHub有一个开源控件PickerView,可以实现 * 联动的效果。虽然该控件使用非常简单,但是填充数据异常繁琐。GitHub上的Demo在填充数据的时候是一条一条地填充的,代码过于冗余。下面提供一种简便的方式来实现。
(1)在app目录下新建一个assets文件夹,将province_data.xml文件复制到该文件夹下,该XML文件里保存了全国的省市县的地址数据。
(2)使用SAX解析xml文件中的数据,并将数据填充到PickerView这个控件中。
—ProvinceModel—
public class ProvinceModel implements IPickerViewData
{
private String name;
private List<CityModel> cityList;
public ProvinceModel()
{
super();
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<CityModel> getCityList()
{
return cityList;
}
public void setCityList(List<CityModel> cityList)
{
this.cityList = cityList;
}
@Override
public String getPickerViewText()
{
return name;
}
}
—CityModel—
public class CityModel implements IPickerViewData
{
private String name;
private List<DistrictModel> districtList;
public CityModel()
{
super();
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<DistrictModel> getDistrictList()
{
return districtList;
}
public void setDistrictList(List<DistrictModel> districtList)
{
this.districtList = districtList;
}
@Override
public String toString()
{
return name;
}
@Override
public String getPickerViewText()
{
return name;
}
}
—DistrictModel—
public class DistrictModel implements IPickerViewData
{
private String name;
private String zipcode;
public DistrictModel()
{
super();
}
public String getZipcode()
{
return zipcode;
}
public void setZipcode(String zipcode)
{
this.zipcode = zipcode;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return name;
}
@Override
public String getPickerViewText()
{
return name;
}
}
—XmlParserHandler—
public class XmlParserHandler extends DefaultHandler
{
/**
* 存储所有的解析对象
*/
private List<ProvinceModel> provinceList = new ArrayList<>();
private ProvinceModel provinceModel = new ProvinceModel();
private CityModel cityModel = new CityModel();
private DistrictModel districtModel = new DistrictModel();
public XmlParserHandler(){}
public List<ProvinceModel> getDataList()
{
return provinceList;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
}
@Override
public void endDocument() throws SAXException
{
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if (qName.equals("province"))
{
provinceModel = new ProvinceModel();
provinceModel.setName(attributes.getValue(0));
provinceModel.setCityList(new ArrayList<CityModel>());
}
else if (qName.equals("city"))
{
cityModel = new CityModel();
cityModel.setName(attributes.getValue(0));
cityModel.setDistrictList(new ArrayList<DistrictModel>());
}
else if (qName.equals("district"))
{
districtModel = new DistrictModel();
districtModel.setName(attributes.getValue(0));
districtModel.setZipcode(attributes.getValue(1));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if(qName.equals("district"))
{
cityModel.getDistrictList().add(districtModel);
}
else if(qName.equals("city"))
{
provinceModel.getCityList().add(cityModel);
}
else if(qName.equals("province"))
{
provinceList.add(provinceModel);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
super.characters(ch, start, length);
}
}
public class AddressAddActivity extends BaseActivity
{
private OptionsPickerView mCityPickerView;
private ArrayList<ProvinceModel> mProvinces = new ArrayList<>();
private ArrayList<ArrayList<CityModel>> mCities = new ArrayList<>();
private ArrayList<ArrayList<ArrayList<DistrictModel>>> mDistricts = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_address_add);
ViewUtils.inject(this);
init();
}
private void init()
{
initProvinceDatas();
try
{
//选项选择器
mCityPickerView = new OptionsPickerView(this);
mCityPickerView.setPicker( mProvinces, mCities, mDistricts, true);
mCityPickerView.setTitle("选择城市");
mCityPickerView.setCyclic(false, false , false);
mCityPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int option2, int options3)
{
String address = mProvinces.get(options1).getName() + " "
+ mCities.get(options1).get(option2) + " "
+ mDistricts.get(options1).get(option2).get(options3);
mTxtAddress.setText(address);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
@OnClick(R.id.ll_city_picker)
public void showpickerView(View view)
{
mCityPickerView.show();
}
protected void initProvinceDatas()
{
AssetManager asset = getAssets();
try
{
InputStream input = asset.open("province_data.xml");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XmlParserHandler handler = new XmlParserHandler();
parser.parse(input , handler);
input.close();
//获取解析出来的数据
mProvinces = (ArrayList<ProvinceModel>) handler.getDataList();
}
catch (Throwable e)
{
e.printStackTrace();
}
if (mProvinces != null)
{
for (ProvinceModel p : mProvinces)
{
List<CityModel> cities = p.getCityList();
//组装城市数据
mCities.add((ArrayList<CityModel>) cities);
ArrayList<ArrayList<DistrictModel>> dts = new ArrayList<>();
for (CityModel c : cities)
{
List<DistrictModel> districts = c.getDistrictList();
dts.add((ArrayList<DistrictModel>) districts);
}
mDistricts.add(dts);
}
}
}
}
到此完毕,很简单的例子。
来源:https://blog.csdn.net/qq_33022345/article/details/53053767
标签:Android,PickerView, , 联动
0
投稿
猜你喜欢
C#中Lambda表达式的用法
2022-07-29 17:29:56
解决Unity无限滚动复用列表的问题
2022-12-26 04:42:18
详解java中String、StringBuilder、StringBuffer的区别
2023-06-17 06:03:23
Java super关键字的使用详解
2021-11-30 13:40:43
C# 线程安全详解
2023-02-07 10:40:46
C#下解析HTML的两种方法介绍
2021-07-03 16:08:21
Java设计模式之动态代理模式实例分析
2022-07-07 17:55:07
Ubuntu16.04 LTS 下安装 Android Studio 2.2.2 的详细步骤
2022-08-11 09:59:22
C#单例类的实现方法
2023-12-01 02:13:30
Java语言读取配置文件config.properties的方法讲解
2023-09-29 14:45:51
解决SpringBoot加载application.properties配置文件的坑
2023-02-05 19:55:27
Java总结篇系列:Java泛型详解
2023-01-25 03:42:58
SpringBoot集成阿里云OSS图片上传
2021-08-15 21:01:55
详解Spring 中 Bean 的生命周期
2023-09-13 14:55:57
MyBatis找不到mapper文件的实现
2023-12-15 09:22:11
IDEA 2021.2 激活教程及启动报错问题解决方法
2023-11-14 14:10:27
Java数据结构之常见排序算法(下)
2022-05-03 12:49:51
C#实现23种常见的设计模式的示例详解
2022-10-19 20:28:29
Android SearchView搜索框组件的使用方法
2023-06-25 08:13:42
C#使用iTextSharp添加PDF水印
2021-10-09 08:59:27