最近按照公司需要,写了一个按照城市首字母排序的demo,原理就是获取城市名称,然后将城市名称转换为相应的拼音,通过对拼音的排序进而得到一个序列,实现了按照首字母分类的功能。
上代码:
由于需要添加省的一些信息,此处给出了绑定省的方法
取得完数据之后绑定到apapter上
获得城市信息,此处为假数据,大家可以自行添加自己的服务器端数据:
/*
* 绑定城市信息,此处为假数据
*/
public void getcityData()
{
HashMap<String, Object> hashcityMap=new HashMap<String, Object>();
hashcityMap.put(CITYID, "1");
hashcityMap.put(CITYNAME, "青岛");
hashcityMap.put(PROVINCEID, "0");
//hashcityMap.put(SORT_KEY, getPinyin("青岛"));
hashcityMap.put(SORT_KEY, getPinyin(String.valueOf(hashcityMap.get(CITYNAME))));
arrayList.add(hashcityMap);
hashcityMap=new HashMap<String, Object>();
hashcityMap.put(CITYID,"2");
hashcityMap.put(CITYNAME, "济南");
hashcityMap.put(PROVINCEID, "0");
//hashcityMap.put(SORT_KEY, getPinyin("济南"));
hashcityMap.put(SORT_KEY, getPinyin(String.valueOf(hashcityMap.get(CITYNAME))));
arrayList.add(hashcityMap);
hashcityMap=new HashMap<String, Object>();
hashcityMap.put(CITYID, "3");
hashcityMap.put(CITYNAME, "日照");
hashcityMap.put(PROVINCEID,"0");
//hashcityMap.put(SORT_KEY, getPinyin("日照"));
hashcityMap.put(SORT_KEY, getPinyin(String.valueOf(hashcityMap.get(CITYNAME))));
arrayList.add(hashcityMap);
hashcityMap=new HashMap<String, Object>();
hashcityMap.put(CITYID, "4");
hashcityMap.put(CITYNAME, "济宁");
hashcityMap.put(PROVINCEID, "0");
//hashcityMap.put(SORT_KEY, getPinyin("济宁"));
hashcityMap.put(SORT_KEY, getPinyin(String.valueOf(hashcityMap.get(CITYNAME))));
arrayList.add(hashcityMap);
hashcityMap=new HashMap<String, Object>();
hashcityMap.put(CITYID, "5");
hashcityMap.put(CITYNAME, "武汉");
hashcityMap.put(PROVINCEID, "1");
//hashcityMap.put(SORT_KEY, getPinyin("济宁"));
hashcityMap.put(SORT_KEY, getPinyin(String.valueOf(hashcityMap.get(CITYNAME))));
arrayList.add(hashcityMap);
Collections.sort(arrayList,comparator);
}
并且添加完数据后还需要对其进行排序
排序函数Collections.sort(arrayList,comparator);
Comparator<HashMap<String, Object>> comparator = new Comparator<HashMap<String, Object>>() {
public int compare(HashMap<String, Object> s1,
HashMap<String, Object> s2) {
String str1 = s1.get(SORT_KEY).toString();
String str2 = s2.get(SORT_KEY).toString();
if (str1.compareTo(str2)>0){
return 1;
}
else {
return -1;
}
}
};
由于需要添加省的一些信息,此处给出了绑定省的方法
/*
* 绑定provinceid到省,此处为假数据
*/
public void getprovinceData()
{
provincenameHashMap.put("0", "山东");
provincenameHashMap.put("1", "湖北");
}
取得完数据之后绑定到apapter上
if (arrayList != null && arrayList.size() > 0) {
List<ContentValues> list = new ArrayList<ContentValues>();
for (int i = 0; i < arrayList.size(); i++) {
HashMap<String, Object> hashMap=new HashMap<String, Object>();
hashMap=arrayList.get(i);
ContentValues cv = new ContentValues();
String cityname = (String)hashMap.get(CITYNAME);
Log.e("cityname",cityname);
String cityid = (String)hashMap.get(CITYID);
String provinceid = (String)hashMap.get(PROVINCEID);
String sortKey = (String)hashMap.get(SORT_KEY);
cv.put(CITYNAME, cityname);
cv.put(CITYID, cityid);
cv.put(PROVINCEID, cityid);
cv.put(SORT_KEY, sortKey);
cv.put(PROVINCENAME, String.valueOf(provincenameHashMap.get(provinceid)));