Skip to content

Commit a9bef5b

Browse files
committed
地址选择器新增使用不同的数据源解析的demo
1 parent fe37e92 commit a9bef5b

5 files changed

Lines changed: 181 additions & 7 deletions

File tree

‎app/src/main/assets/city.txt‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package cn.qqtheme.androidpicker;
2+
3+
import android.app.Activity;
4+
import android.app.ProgressDialog;
5+
import android.os.AsyncTask;
6+
import android.support.annotation.WorkerThread;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import cn.qqtheme.framework.entity.City;
12+
import cn.qqtheme.framework.entity.County;
13+
import cn.qqtheme.framework.entity.Province;
14+
import cn.qqtheme.framework.util.ConvertUtils;
15+
16+
/**
17+
* 获取地址数据
18+
*
19+
* @author 李玉江[QQ:1032694760]
20+
* @since 2017/10/13
21+
*/
22+
public class AddressInitTask extends AsyncTask<Void, Void, ArrayList<Province>> {
23+
private Activity activity;
24+
private ProgressDialog dialog;
25+
private InitCallback callback;
26+
private ArrayList<Province> provinces;
27+
28+
public AddressInitTask(Activity activity, InitCallback callback) {
29+
this.activity = activity;
30+
this.callback = callback;
31+
}
32+
33+
@Override
34+
protected void onPreExecute() {
35+
dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
36+
}
37+
38+
@Override
39+
protected ArrayList<Province> doInBackground(Void... params) {
40+
try {
41+
String data = ConvertUtils.toString(activity.getAssets().open("city.txt"));
42+
return parseData(data);
43+
} catch (java.io.IOException e) {
44+
e.printStackTrace();
45+
return null;
46+
}
47+
}
48+
49+
@Override
50+
protected void onPostExecute(ArrayList<Province> result) {
51+
dialog.dismiss();
52+
if (result == null || result.size() == 0) {
53+
callback.onDataInitFailure();
54+
} else {
55+
callback.onDataInitSuccess(result);
56+
}
57+
}
58+
59+
@WorkerThread
60+
private ArrayList<Province> parseData(String data) {
61+
if (provinces != null && provinces.size() > 0) {
62+
return provinces;
63+
}
64+
provinces = new ArrayList<>();
65+
String[] fullCodeAndNames = data.split(";");
66+
for (String fullCodeAndName : fullCodeAndNames) {
67+
String[] codeAndName = fullCodeAndName.split(",");
68+
if (codeAndName.length != 2) {
69+
continue;
70+
}
71+
String code = codeAndName[0];
72+
String name = codeAndName[1];
73+
if (code.substring(2, 6).equals("0000")) {
74+
//省份
75+
Province province = new Province();
76+
province.setAreaId(code);
77+
province.setAreaName(name);
78+
province.setCities(new ArrayList<City>());
79+
provinces.add(province);
80+
} else if (code.substring(4, 6).equals("00")) {
81+
//地市
82+
Province province = findProvinceByCode(code.substring(0, 2));
83+
if (province != null) {
84+
City city = new City();
85+
city.setAreaId(code);
86+
city.setAreaName(name);
87+
city.setCounties(new ArrayList<County>());
88+
province.getCities().add(city);
89+
}
90+
} else {
91+
//区县
92+
City city = findCityByCode(code.substring(0, 2), code.substring(2, 4));
93+
if (city != null) {
94+
County county = new County();
95+
county.setAreaId(code);
96+
county.setAreaName(name);
97+
city.getCounties().add(county);
98+
}
99+
}
100+
}
101+
return provinces;
102+
}
103+
104+
private Province findProvinceByCode(String provinceCode) {
105+
for (Province province : provinces) {
106+
if (province.getAreaId().substring(0, 2).equals(provinceCode)) {
107+
return province;
108+
}
109+
}
110+
return null;
111+
}
112+
113+
private City findCityByCode(String provinceCode, String cityCode) {
114+
for (Province province : provinces) {
115+
List<City> cities = province.getCities();
116+
for (City city : cities) {
117+
if (province.getAreaId().substring(0, 2).equals(provinceCode) &&
118+
city.getAreaId().substring(2, 4).equals(cityCode)) {
119+
return city;
120+
}
121+
}
122+
}
123+
return null;
124+
}
125+
126+
public interface InitCallback {
127+
128+
void onDataInitFailure();
129+
130+
void onDataInitSuccess(ArrayList<Province> provinces);
131+
132+
}
133+
134+
}

‎app/src/main/java/cn/qqtheme/androidpicker/AddressPickTask.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected ArrayList<Province> doInBackground(String... params) {
7171
try {
7272
String json = ConvertUtils.toString(activity.getAssets().open("city.json"));
7373
data.addAll(JSON.parseArray(json, Province.class));
74-
} catch (Exception e) {
74+
} catch (java.io.IOException e) {
7575
e.printStackTrace();
7676
}
7777
return data;
@@ -85,11 +85,9 @@ protected void onPostExecute(ArrayList<Province> result) {
8585
picker.setHideProvince(hideProvince);
8686
picker.setHideCounty(hideCounty);
8787
if (hideCounty) {
88-
picker.setColumnWeight(0.8f, 1.0f);
89-
} else if (hideProvince) {
90-
picker.setColumnWeight(1.0f, 0.8f);
88+
picker.setColumnWeight(1 / 3.0f, 2 / 3.0f);//将屏幕分为3份,省级和地级的比例为1:2
9189
} else {
92-
picker.setColumnWeight(0.8f, 1.0f, 1.0f);
90+
picker.setColumnWeight(2 / 8.0f, 3 / 8.0f, 3 / 8.0f);//省级、地级和县级的比例为2:3:3
9391
}
9492
picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);
9593
picker.setOnAddressPickListener(callback);

‎app/src/main/java/cn/qqtheme/androidpicker/MainActivity.java‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void onYearMonthDayTimePicker(View view) {
145145
picker.setDateRangeEnd(2025, 11, 11);
146146
picker.setTimeRangeStart(9, 0);
147147
picker.setTimeRangeEnd(20, 30);
148+
picker.setTopLineColor(0x99FF0000);
148149
picker.setLabelTextColor(0xFFFF0000);
149150
picker.setDividerColor(0xFFFF0000);
150151
picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
@@ -175,6 +176,7 @@ public void onDatePicked(String year, String month) {
175176

176177
public void onMonthDayPicker(View view) {
177178
DatePicker picker = new DatePicker(this, DatePicker.MONTH_DAY);
179+
picker.setUseWeight(false);
178180
picker.setGravity(Gravity.CENTER);//弹框居中
179181
picker.setRangeStart(5, 1);
180182
picker.setRangeEnd(12, 31);
@@ -190,7 +192,7 @@ public void onDatePicked(String month, String day) {
190192

191193
public void onTimePicker(View view) {
192194
TimePicker picker = new TimePicker(this, TimePicker.HOUR_24);
193-
picker.setUseWeight(true);
195+
picker.setUseWeight(false);
194196
picker.setCycleDisable(false);
195197
picker.setRangeStart(0, 0);//00:00
196198
picker.setRangeEnd(23, 59);//23:59
@@ -477,7 +479,6 @@ public void onAddressPicked(Province province, City city, County county) {
477479
}
478480
}
479481

480-
481482
public void onAddress3Picker(View view) {
482483
AddressPickTask task = new AddressPickTask(this);
483484
task.setHideCounty(true);
@@ -495,6 +496,40 @@ public void onAddressPicked(Province province, City city, County county) {
495496
task.execute("四川", "阿坝");
496497
}
497498

499+
public void onAddress4Picker(View view) {
500+
new AddressInitTask(this, new AddressInitTask.InitCallback() {
501+
@Override
502+
public void onDataInitFailure() {
503+
showToast("数据初始化失败");
504+
}
505+
506+
@Override
507+
public void onDataInitSuccess(ArrayList<Province> provinces) {
508+
AddressPicker picker = new AddressPicker(activity, provinces);
509+
picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
510+
@Override
511+
public void onAddressPicked(Province province, City city, County county) {
512+
String provinceName = province.getName();
513+
String cityName = "";
514+
if (city != null) {
515+
cityName = city.getName();
516+
//忽略直辖市的二级名称
517+
if (cityName.equals("市辖区") || cityName.equals("市") || cityName.equals("县")) {
518+
cityName = "";
519+
}
520+
}
521+
String countyName = "";
522+
if (county != null) {
523+
countyName = county.getName();
524+
}
525+
showToast(provinceName + " " + cityName + " " + countyName);
526+
}
527+
});
528+
picker.show();
529+
}
530+
}).execute();
531+
}
532+
498533
public void onColorPicker(View view) {
499534
ColorPicker picker = new ColorPicker(this);
500535
picker.setInitColor(0xDD00DD);

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
android:onClick="onAddressPicker"
126126
android:text="地址选择(包括省级、地级、县级)" />
127127

128+
<Button
129+
android:layout_width="match_parent"
130+
android:layout_height="wrap_content"
131+
android:onClick="onAddress4Picker"
132+
android:text="地址选择(使用不同的数据源解析)" />
133+
128134
<Button
129135
android:layout_width="match_parent"
130136
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)