Comments (3)
可以试试修改下面两处,得到地址后,根据地址(areaName)从ArrayList<AddressPicker.Province>中获得区号(areaId):
ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
try {
String json = AssetsUtils.readText(activity, "city.json");
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
} catch (Exception e) {
e.printStackTrace();
}
@Override
public void onAddressPicked(String province, String city, String county) {
if (county==null){
Toast.makeText(activity, province + city, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, province + city + county, Toast.LENGTH_LONG).show();
}
}
from androidpicker.
感谢您的回复。我是这样修改的。
package com.hiayi.dialog.wheelpicker.picker;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import com.hiayi.dialog.wheelpicker.widget.WheelView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
-
地址选择器(包括省级、地级、县级)。
-
地址数据见示例项目的“city.json”,来源于国家统计局官网(http://www.stats.gov.cn/tjsj/tjbz/xzqhdm)
/
public class AddressPicker extends LinkagePicker {
private OnAddressPickListener onAddressPickListener;
private boolean hideProvince = false;
private boolean hideCounty = false;
/*- 获取城市编码
/
private boolean hasCityCode = false;
/* - 城市编码
/
private String cityCode;
/* - 城市编码数据
*/
private HashMap<Integer, List> cityMap = new HashMap<Integer, List>();
public AddressPicker(Activity activity, ArrayList data, boolean obtainCityCode) {
super(activity);
this.hasCityCode = obtainCityCode;
parseData(data);
} - 获取城市编码
/**
* Instantiates a new Address picker.
*
* @param activity the activity
* @param data the data
*/
public AddressPicker(Activity activity, ArrayList data) {
super(activity);
parseData(data);
}
private void parseData(ArrayList data) {
int provinceSize = data.size();
//添加省
for (int x = 0; x < provinceSize; x++) {
Province pro = data.get(x);
firstList.add(pro.getAreaName());
ArrayList cities = pro.getCities();
ArrayList xCities = new ArrayList();
if (hasCityCode) {
cityMap.put(x, cities);
}
ArrayList<ArrayList> xCounties = new ArrayList<ArrayList>();
int citySize = cities.size();
//添加地市
for (int y = 0; y < citySize; y++) {
City cit = cities.get(y);
xCities.add(cit.getAreaName());
ArrayList counties = cit.getCounties();
ArrayList yCounties = new ArrayList();
int countySize = counties.size();
//添加区县
if (countySize == 0) {
yCounties.add(cit.getAreaName());
} else {
for (int z = 0; z < countySize; z++) {
yCounties.add(counties.get(z).getAreaName());
}
}
xCounties.add(yCounties);
}
secondList.add(xCities);
thirdList.add(xCounties);
}
}
/**
* Sets selected item.
*
* @param province the province
* @param city the city
* @param county the county
*/
public void setSelectedItem(String province, String city, String county) {
super.setSelectedItem(province, city, county);
}
/**
* 隐藏省级行政区,只显示地市级和区县级。
* 设置为true的话,地址数据中只需要某个省份的即可
* 参见示例中的“city2.json”
*
* @param hideProvince the hide province
*/
public void setHideProvince(boolean hideProvince) {
this.hideProvince = hideProvince;
}
/**
* 隐藏县级行政区,只显示省级和市级。
* 设置为true的话,hideProvince将强制为false
* 数据源依然使用“city.json” 仅在逻辑上隐藏县级选择框。
*
* @param hideCounty the hide county
*/
public void setHideCounty(boolean hideCounty) {
this.hideCounty = hideCounty;
}
/**
* Sets on address pick listener.
*
* @param listener the listener
*/
public void setOnAddressPickListener(OnAddressPickListener listener) {
this.onAddressPickListener = listener;
}
@deprecated
@OverRide
public void setOnLinkageListener(OnLinkageListener onLinkageListener) {
throw new UnsupportedOperationException("Please use setOnAddressPickListener instead.");
}
@OverRide
@nonnull
protected View makeCenterView() {
if (hideCounty) {
hideProvince = false;
}
if (firstList.size() == 0) {
throw new IllegalArgumentException("please initial data at first, can't be empty");
}
LinearLayout layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
final WheelView provinceView = new WheelView(activity);
final int width = screenWidthPixels / 3;
provinceView.setLayoutParams(new LinearLayout.LayoutParams(width, WRAP_CONTENT));
provinceView.setTextSize(textSize);
provinceView.setTextColor(textColorNormal, textColorFocus);
provinceView.setLineVisible(lineVisible);
provinceView.setLineColor(lineColor);
provinceView.setOffset(offset);
layout.addView(provinceView);
if (hideProvince) {
provinceView.setVisibility(View.GONE);
}
final WheelView cityView = new WheelView(activity);
cityView.setLayoutParams(new LinearLayout.LayoutParams(width, WRAP_CONTENT));
cityView.setTextSize(textSize);
cityView.setTextColor(textColorNormal, textColorFocus);
cityView.setLineVisible(lineVisible);
cityView.setLineColor(lineColor);
cityView.setOffset(offset);
layout.addView(cityView);
final WheelView countyView = new WheelView(activity);
countyView.setLayoutParams(new LinearLayout.LayoutParams(width, WRAP_CONTENT));
countyView.setTextSize(textSize);
countyView.setTextColor(textColorNormal, textColorFocus);
countyView.setLineVisible(lineVisible);
countyView.setLineColor(lineColor);
countyView.setOffset(offset);
layout.addView(countyView);
if (hideCounty) {
countyView.setVisibility(View.GONE);
}
provinceView.setItems(firstList, selectedFirstIndex);
provinceView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@OverRide
public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
selectedFirstText = item;
selectedFirstIndex = selectedIndex;
selectedThirdIndex = 0;
//根据省份获取地市
cityView.setItems(secondList.get(selectedFirstIndex), isUserScroll ? 0 : selectedSecondIndex);
//根据地市获取区县
countyView.setItems(thirdList.get(selectedFirstIndex).get(0), isUserScroll ? 0 : selectedThirdIndex);
}
});
cityView.setItems(secondList.get(selectedFirstIndex), selectedSecondIndex);
cityView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@OverRide
public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
selectedSecondText = item;
selectedSecondIndex = selectedIndex;
if (hasCityCode) {
cityCode = cityMap.get(selectedFirstIndex).get(selectedSecondIndex).areaId;
}
//根据地市获取区县
countyView.setItems(thirdList.get(selectedFirstIndex).get(selectedSecondIndex), isUserScroll ? 0 : selectedThirdIndex);
}
});
countyView.setItems(thirdList.get(selectedFirstIndex).get(selectedSecondIndex), selectedThirdIndex);
countyView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@OverRide
public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
selectedThirdText = item;
selectedThirdIndex = selectedIndex;
}
});
return layout;
}
@OverRide
public void onSubmit() {
if (onAddressPickListener != null) {
if (hideCounty) {
if (hasCityCode) {
clearCityData();
onAddressPickListener.onAddressPicked(selectedFirstText, selectedSecondText, null, cityCode);
} else {
onAddressPickListener.onAddressPicked(selectedFirstText, selectedSecondText, null);
}
} else {
if (hasCityCode) {
clearCityData();
onAddressPickListener.onAddressPicked(selectedFirstText, selectedSecondText, selectedThirdText, cityCode);
} else {
onAddressPickListener.onAddressPicked(selectedFirstText, selectedSecondText, selectedThirdText);
}
}
}
}
/**
* 清理数据
*/
private void clearCityData() {
if (cityMap != null) {
cityMap.clear();
}
}
/**
* The interface On address pick listener.
*/
public interface OnAddressPickListener {
/**
* On address picked.
*
* @param province the province
* @param city the city
* @param county the county ,if {@hideCounty} is true,this is null
*/
void onAddressPicked(String province, String city, String county);
void onAddressPicked(String province, String city, String county, String cityCode);
}
/**
* The type Area.
/
public abstract static class Area {
/*
* The Area id.
/
String areaId;
/*
* The Area name.
*/
String areaName;
/**
* Gets area id.
*
* @return the area id
*/
public String getAreaId() {
return areaId;
}
/**
* Sets area id.
*
* @param areaId the area id
*/
public void setAreaId(String areaId) {
this.areaId = areaId;
}
/**
* Gets area name.
*
* @return the area name
*/
public String getAreaName() {
return areaName;
}
/**
* Sets area name.
*
* @param areaName the area name
*/
public void setAreaName(String areaName) {
this.areaName = areaName;
}
@OverRide
public String toString() {
return "areaId=" + areaId + ",areaName=" + areaName;
}
}
/**
* The type Province.
/
public static class Province extends Area {
/*
* The Cities.
*/
ArrayList cities = new ArrayList();
/**
* Gets cities.
*
* @return the cities
*/
public ArrayList getCities() {
return cities;
}
/**
* Sets cities.
*
* @param cities the cities
*/
public void setCities(ArrayList cities) {
this.cities = cities;
}
}
/**
* The type City.
*/
public static class City extends Area {
private ArrayList counties = new ArrayList();
/**
* Gets counties.
*
* @return the counties
*/
public ArrayList getCounties() {
return counties;
}
/**
* Sets counties.
*
* @param counties the counties
*/
public void setCounties(ArrayList counties) {
this.counties = counties;
}
}
/**
* The type County.
*/
public static class County extends Area {
}
}
在 2016-05-24 06:52:05,"穿青人" [email protected] 写道:
可以试试修改下面两处,得到地址后,根据地址(areaName)从ArrayList中获得区号(areaId):
ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
try {
String json = AssetsUtils.readText(activity, "city.json");
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
} catch (Exception e) {
e.printStackTrace();
}
@OverRide
public void onAddressPicked(String province, String city, String county) {
if (county==null){
Toast.makeText(activity, province + city, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, province + city + county, Toast.LENGTH_LONG).show();
}
}
—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
from androidpicker.
最新版已经支持获取区号
from androidpicker.
Related Issues (20)
- ImagePicker在Fragment上有使用示例吗? HOT 2
- 12小时制setRange不对问题
- 日历日期选择多选时范围指定前几几个月 选择的时候默认显示的是开始的日期 HOT 1
- 更新 china_address.json 的操作方法,获取到最新版的省市区数据 HOT 1
- TimeMode设置为None时,DatimeWheelLayout显示内容无法居中 HOT 1
- 在时间选择器中 上下滚动夸循环日期时,是否可以让月份随之加减 HOT 2
- 滚轮选择器-数字选择器 当参数为float型时,会出现精度丢失问题 HOT 1
- 避免maskView导致的内存泄露 HOT 1
- 圆角和样式 HOT 1
- 可以不保留分秒吗? HOT 2
- 支持registerForActivityResult
- 关于联动
- 文字展示不下不能换行 HOT 2
- 在一些RTL的语言例如印度,ok和cancel重叠在一起,请问如何解决
- 乱码文件名导致应用崩溃 HOT 1
- 当指示器size设置为1dp的时候不显示 HOT 1
- 滑动过程中获取下标错误
- DatimeWheelLayout 开始时间范围设为1年1月1日 0 时0分 年份显示会显示1001 不是1
- 腕表开发上二级联动列表滚轮无法弹出
- 日期选择建议加上 阳历 阴历 切换
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidpicker.