-
Notifications
You must be signed in to change notification settings - Fork 636
/
CityListSelectActivity.java
262 lines (207 loc) · 8.47 KB
/
CityListSelectActivity.java
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
package com.lljjcoder.style.citylist;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.github.promeg.pinyinhelper.Pinyin;
import com.lljjcoder.style.citylist.bean.CityInfoBean;
import com.lljjcoder.style.citylist.sortlistview.CharacterParser;
import com.lljjcoder.style.citylist.sortlistview.PinyinComparator;
import com.lljjcoder.style.citylist.sortlistview.SideBar;
import com.lljjcoder.style.citylist.sortlistview.SortAdapter;
import com.lljjcoder.style.citylist.sortlistview.SortModel;
import com.lljjcoder.style.citylist.utils.CityListLoader;
import com.lljjcoder.style.citylist.widget.CleanableEditView;
import com.lljjcoder.style.citypickerview.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CityListSelectActivity extends Activity {
CleanableEditView mCityTextSearch;
TextView mCurrentCityTag;
TextView mCurrentCity;
TextView mLocalCityTag;
TextView mLocalCity;
ListView sortListView;
TextView mDialog;
SideBar mSidrbar;
ImageView imgBack;
public SortAdapter adapter;
/**
* 汉字转换成拼音的类
*/
private CharacterParser characterParser;
private List<SortModel> sourceDateList;
/**
* 根据拼音来排列ListView里面的数据类
*/
private PinyinComparator pinyinComparator;
private List<CityInfoBean> cityListInfo = new ArrayList<>();
private CityInfoBean cityInfoBean = new CityInfoBean();
//startActivityForResult flag
public static final int CITY_SELECT_RESULT_FRAG = 0x0000032;
public static List<CityInfoBean> sCityInfoBeanList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_list_select);
initView();
initList();
setCityData(CityListLoader.getInstance().getCityListData());
}
private void initView() {
mCityTextSearch = (CleanableEditView) findViewById(R.id.cityInputText);
mCurrentCityTag = (TextView) findViewById(R.id.currentCityTag);
mCurrentCity = (TextView) findViewById(R.id.currentCity);
mLocalCityTag = (TextView) findViewById(R.id.localCityTag);
mLocalCity = (TextView) findViewById(R.id.localCity);
sortListView = (ListView) findViewById(R.id.country_lvcountry);
mDialog = (TextView) findViewById(R.id.dialog);
mSidrbar = (SideBar) findViewById(R.id.sidrbar);
imgBack = (ImageView) findViewById(R.id.imgBack);
imgBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void setCityData(List<CityInfoBean> cityList) {
cityListInfo = cityList;
if (cityListInfo == null) {
return;
}
int count = cityList.size();
String[] list = new String[count];
for (int i = 0; i < count; i++)
list[i] = cityList.get(i).getName();
sourceDateList.addAll(filledData(cityList));
// 根据a-z进行排序源数据
Collections.sort(sourceDateList, pinyinComparator);
adapter.notifyDataSetChanged();
}
/**
* 为ListView填充数据
*
* @return
*/
private List<SortModel> filledData(List<CityInfoBean> cityList) {
List<SortModel> mSortList = new ArrayList<SortModel>();
for (int i = 0; i < cityList.size(); i++) {
CityInfoBean result = cityList.get(i);
if (result != null) {
SortModel sortModel = new SortModel();
String cityName = result.getName();
//汉字转换成拼音
if (!TextUtils.isEmpty(cityName) && cityName.length() > 0) {
// String pinyin = "";
// if (cityName.equals("重庆市")) {
// pinyin = "chong";
// }
// else if (cityName.equals("长沙市")) {
// pinyin = "chang";
// }
// else if (cityName.equals("长春市")) {
// pinyin = "chang";
// }
// else {
// pinyin = mPinYinUtils.getStringPinYin(cityName.substring(0, 1));
// }
//
String pinyin = Pinyin.toPinyin(cityName.substring(0, 1), "").toLowerCase();
if (!TextUtils.isEmpty(pinyin)) {
sortModel.setName(cityName);
String sortString = pinyin.substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if (sortString.matches("[A-Z]")) {
sortModel.setSortLetters(sortString.toUpperCase());
} else {
sortModel.setSortLetters("#");
}
mSortList.add(sortModel);
} else {
Log.d("citypicker_log", "null,cityName:-> " + cityName + " pinyin:-> " + pinyin);
}
}
}
}
return mSortList;
}
private void initList() {
sourceDateList = new ArrayList<SortModel>();
adapter = new SortAdapter(CityListSelectActivity.this, sourceDateList);
sortListView.setAdapter(adapter);
//实例化汉字转拼音类
characterParser = CharacterParser.getInstance();
pinyinComparator = new PinyinComparator();
mSidrbar.setTextView(mDialog);
//设置右侧触摸监听
mSidrbar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
//该字母首次出现的位置
int position = adapter.getPositionForSection(s.charAt(0));
if (position != -1) {
sortListView.setSelection(position);
}
}
});
sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String cityName = ((SortModel) adapter.getItem(position)).getName();
cityInfoBean = CityInfoBean.findCity(cityListInfo, cityName);
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("cityinfo", cityInfoBean);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
//根据输入框输入值的改变来过滤搜索
mCityTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
filterData(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
/**
* 根据输入框中的值来过滤数据并更新ListView
*
* @param filterStr
*/
private void filterData(String filterStr) {
List<SortModel> filterDateList = new ArrayList<SortModel>();
if (TextUtils.isEmpty(filterStr)) {
filterDateList = sourceDateList;
} else {
filterDateList.clear();
for (SortModel sortModel : sourceDateList) {
String name = sortModel.getName();
if (name.contains(filterStr) || characterParser.getSelling(name).startsWith(filterStr)) {
filterDateList.add(sortModel);
}
}
}
// 根据a-z进行排序
Collections.sort(filterDateList, pinyinComparator);
adapter.updateListView(filterDateList);
}
}