22
33import android .app .Activity ;
44import android .support .annotation .NonNull ;
5+ import android .support .annotation .Nullable ;
56import android .view .Gravity ;
67import android .view .View ;
78import android .widget .LinearLayout ;
1617import cn .qqtheme .framework .widget .WheelView ;
1718
1819/**
19- * 地址选择器(包括省级、地级、县级),地址数据见示例项目assets目录下 。
20+ * 地址选择器(包括省级、地级、县级),地址数据见demo项目assets目录下 。
2021 * “assets/city.json”转换自国家统计局(http://www.stats.gov.cn/tjsj/tjbz/xzqhdm)
21- * “assets/area.db”来源于开源项目(https://github.com/chihane/JDAddressSelector)
2222 *
2323 * @author 李玉江[QQ:1032694760]
2424 * @see Province
@@ -52,10 +52,12 @@ public void setSelectedItem(String province, String city, String county) {
5252 setSelectedItem (new Province (province ), new City (city ), new County (county ));
5353 }
5454
55+ @ NonNull
5556 public Province getSelectedProvince () {
5657 return provinces .get (selectedFirstIndex );
5758 }
5859
60+ @ Nullable
5961 public City getSelectedCity () {
6062 List <City > cities = getSelectedProvince ().getCities ();
6163 if (cities .size () == 0 ) {
@@ -64,8 +66,13 @@ public City getSelectedCity() {
6466 return cities .get (selectedSecondIndex );
6567 }
6668
69+ @ Nullable
6770 public County getSelectedCounty () {
68- List <County > counties = getSelectedCity ().getCounties ();
71+ City selectedCity = getSelectedCity ();
72+ if (selectedCity == null ) {
73+ return null ;
74+ }
75+ List <County > counties = selectedCity .getCounties ();
6976 if (counties .size () == 0 ) {
7077 return null ;//可能没有第三级数据
7178 }
@@ -75,7 +82,7 @@ public County getSelectedCounty() {
7582 /**
7683 * 隐藏省级行政区,只显示地市级和区县级。
7784 * 设置为true的话,地址数据中只需要某个省份的即可
78- * 参见示例中的 “assets/city2.json”
85+ * 参见demo中的 “assets/city2.json”
7986 */
8087 public void setHideProvince (boolean hideProvince ) {
8188 this .hideProvince = hideProvince ;
@@ -84,7 +91,7 @@ public void setHideProvince(boolean hideProvince) {
8491 /**
8592 * 隐藏县级行政区,只显示省级和市级。
8693 * 设置为true的话,hideProvince将强制为false
87- * 数据源依然使用 “assets/city.json” 仅在逻辑上隐藏县级选择框,实际项目中应该去掉县级数据。
94+ * 数据源依然使用demo中的 “assets/city.json” 仅在逻辑上隐藏县级选择框,实际项目中应该去掉县级数据。
8895 */
8996 public void setHideCounty (boolean hideCounty ) {
9097 this .hideCounty = hideCounty ;
@@ -268,13 +275,13 @@ private static class AddressProvider implements Provider<Province, City, County>
268275 private List <List <City >> secondList = new ArrayList <>();
269276 private List <List <List <County >>> thirdList = new ArrayList <>();
270277
271- public AddressProvider (List <Province > provinces ) {
278+ AddressProvider (List <Province > provinces ) {
272279 parseData (provinces );
273280 }
274281
275282 @ Override
276283 public boolean isOnlyTwo () {
277- return thirdList . size () == 0 ;
284+ return false ;
278285 }
279286
280287 @ Override
@@ -286,13 +293,23 @@ public List<Province> initFirstData() {
286293 @ Override
287294 @ NonNull
288295 public List <City > linkageSecondData (int firstIndex ) {
296+ if (secondList .size () <= firstIndex ) {
297+ return new ArrayList <>();
298+ }
289299 return secondList .get (firstIndex );
290300 }
291301
292302 @ Override
293303 @ NonNull
294304 public List <County > linkageThirdData (int firstIndex , int secondIndex ) {
295- return thirdList .get (firstIndex ).get (secondIndex );
305+ if (thirdList .size () <= firstIndex ) {
306+ return new ArrayList <>();
307+ }
308+ List <List <County >> lists = thirdList .get (firstIndex );
309+ if (lists .size () <= secondIndex ) {
310+ return new ArrayList <>();
311+ }
312+ return lists .get (secondIndex );
296313 }
297314
298315 private void parseData (List <Province > data ) {
0 commit comments