AddressAlertDialog.java
8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.drp.mobliemall.utils;
import android.app.Dialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.drp.R;
import com.drp.mobliemall.ui.adapter.ArrayWheelAdapter;
import com.drp.mobliemall.ui.adapter.OnWheelChangedListener;
import com.drp.mobliemall.ui.adapter.WheelView;
import com.drp.mobliemall.ui.widget.ProvinceModel;
import com.drp.mobliemall.ui.widget.DistrictModel;
import com.fish.drp.div.util.UIUtil;
import com.drp.mobliemall.ui.widget.CityModel;
import com.drp.mobliemall.ui.widget.XmlParserHandler;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class AddressAlertDialog extends Dialog implements
OnWheelChangedListener {
private Button btNegative, btPositive;
/**
* 所有省
*/
protected String[] mProvinceDatas;
/**
* key - 省 value - 市
*/
protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
/**
* key - 市 values - 区
*/
protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();
/**
* key - 区 values - 邮编
*/
protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();
/**
* 当前省的名称
*/
protected String mCurrentProviceName;
/**
* 当前市的名称
*/
protected String mCurrentCityName;
/**
* 当前区的名称
*/
public interface PriorityListener {
/**
* 回调函数,用于在Dialog的监听事件触发后刷新Activity的UI显示
*/
public void refreshPriorityUI(String sheng, String shi, String qu);
}
private PriorityListener listener;
/**
* 带监听器参数的构造函数
*/
protected String mCurrentDistrictName = "";
private WheelView mViewProvince;
private WheelView mViewCity;
private WheelView mViewDistrict;
private Context mContext;
String sheng,shi,qu;
private View view;
public AddressAlertDialog(Context context) {
super(context, R.style.commonDialog);
mContext = context;
}
public AddressAlertDialog(Context context, String pro, String shi, String qu, View address, PriorityListener listener) {
super(context, R.style.commonDialog);
mContext = context;
this.sheng = pro;
this.shi = shi;
this.qu = qu;
view = address;
this.listener = listener;
}
/*public AddressAlertDialog(Context context, String pro, String shi, String qu, View address,DemandFullTimeJob job) {
super(context, R.style.commonDialog);
mContext = context;
this.sheng = pro;
this.shi = shi;
this.qu = qu;
view = address;
this.job=job;
}
public AddressAlertDialog(Context context, String pro, String shi, String qu, View address,MemberInfo memberInfo) {
super(context, R.style.commonDialog);
mContext = context;
this.sheng = pro;
this.shi = shi;
this.qu = qu;
view = address;
this.memberInfo=memberInfo;
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_address_pop);
setCanceledOnTouchOutside(true);
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.width = UIUtil.getScreenWidth(getContext());
lp.gravity = Gravity.BOTTOM;
lp.dimAmount = 0.6f;
this.getWindow().setAttributes(lp);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
btNegative = (Button) findViewById(R.id.cancel);
btPositive = (Button) findViewById(R.id.submit);
btNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
btPositive.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sheng = mCurrentProviceName;
shi = mCurrentCityName;
qu = mCurrentDistrictName;
((TextView) view).setText(mCurrentProviceName+mCurrentCityName+mCurrentDistrictName);
((TextView)view).setTag(mCurrentProviceName+";"+mCurrentCityName+";" + mCurrentDistrictName);
listener.refreshPriorityUI(mCurrentProviceName, mCurrentCityName,mCurrentDistrictName);
dismiss();
}
});
mViewProvince = (WheelView) findViewById(R.id.id_province);
mViewCity = (WheelView) findViewById(R.id.id_city);
mViewDistrict = (WheelView) findViewById(R.id.id_district);
mViewProvince.addChangingListener(this);
mViewCity.addChangingListener(this);
mViewDistrict.addChangingListener(this);
initProvinceDatas();
setUpData();
}
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if(wheel.getId() == R.id.id_province){
mViewCity.setCurrentItem(0);
updateCities();
} else if(wheel.getId() == R.id.id_city){
updateAreas();
} else if(wheel.getId() == R.id.id_district){
mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[mViewDistrict.getCurrentItem()];
}
}
private void setUpData() {
initProvinceDatas();
mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mProvinceDatas));
// 设置可见条目数量
mViewProvince.setVisibleItems(7);
mViewCity.setVisibleItems(7);
mViewDistrict.setVisibleItems(7);
mViewProvince.setCurrentItem(14);
mViewCity.setCurrentItem(5);
updateCities();
updateAreas();
}
private void updateCities() {
int pCurrent = mViewProvince.getCurrentItem();
mCurrentProviceName = mProvinceDatas[pCurrent];
String[] cities = mCitisDatasMap.get(mCurrentProviceName);
if (cities == null) {
cities = new String[] { "" };
}
//装载 mViewCity wheelView中的数据
mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, cities));
//
updateAreas();
}
/**
* 根据当前的市,更新区WheelView的信息
*/
private void updateAreas() {
int pCurrent = mViewCity.getCurrentItem();
mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
String[] areas = mDistrictDatasMap.get(mCurrentCityName);
if (areas == null) {
areas = new String[] { "" };
}
mCurrentDistrictName=areas[0];
mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(mContext, areas));
mViewDistrict.setCurrentItem(0);
}
/**
* 解析省市区的XML数据
*/
protected void initProvinceDatas() {
List<ProvinceModel> provinceList = null;
AssetManager asset = mContext.getAssets();
try {
InputStream input = asset.open("province_data.xml");
// 创建一个解析xml的工厂对象
SAXParserFactory spf = SAXParserFactory.newInstance();
// 解析xml
SAXParser parser = spf.newSAXParser();
XmlParserHandler handler = new XmlParserHandler();
parser.parse(input, handler);
input.close();
// 获取解析出来的数据
provinceList = handler.getDataList();
// */ 初始化默认选中的省、市、区
if (provinceList != null && !provinceList.isEmpty()) {
mCurrentProviceName = provinceList.get(0).getName();
List<CityModel> cityList = provinceList.get(0).getCityList();
if (cityList != null && !cityList.isEmpty()) {
mCurrentCityName = cityList.get(0).getName();
List<DistrictModel> districtList = cityList.get(0)
.getDistrictList();
mCurrentDistrictName = districtList.get(0).getName();
}
}
// */
mProvinceDatas = new String[provinceList.size()];
for (int i = 0; i < provinceList.size(); i++) {
// 遍历所有省的数据
mProvinceDatas[i] = provinceList.get(i).getName();
List<CityModel> cityList = provinceList.get(i).getCityList();
String[] cityNames = new String[cityList.size()];
for (int j = 0; j < cityList.size(); j++) {
// 遍历省下面的所有市的数据
cityNames[j] = cityList.get(j).getName();
List<DistrictModel> districtList = cityList.get(j)
.getDistrictList();
String[] distrinctNameArray = new String[districtList
.size()];
DistrictModel[] distrinctArray = new DistrictModel[districtList
.size()];
for (int k = 0; k < districtList.size(); k++) {
// 遍历市下面所有区/县的数据
DistrictModel districtModel = new DistrictModel(
districtList.get(k).getName(), districtList
.get(k).getZipcode());
// 区/县对于的邮编,保存到mZipcodeDatasMap
mZipcodeDatasMap.put(districtList.get(k).getName(),
districtList.get(k).getZipcode());
distrinctArray[k] = districtModel;
distrinctNameArray[k] = districtModel.getName();
}
// 市-区/县的数据,保存到mDistrictDatasMap
mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
}
// 省-市的数据,保存到mCitisDatasMap
mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
}
}
}