android 实现按照城市首字母(拼音)分类的应用

本文介绍如何在Android应用中实现城市列表按照首字母(拼音)分类。首先获取城市信息,然后使用Collections.sort()函数配合自定义比较器进行排序。最终,展示一个完整且有序的城市列表。
摘要由CSDN通过智能技术生成
最近按照公司需要,写了一个按照城市首字母排序的demo,原理就是获取城市名称,然后将城市名称转换为相应的拼音,通过对拼音的排序进而得到一个序列,实现了按照首字母分类的功能。 上代码:

获得城市信息,此处为假数据,大家可以自行添加自己的服务器端数据:

/*
	 * 绑定城市信息,此处为假数据
	 */
	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)));
			
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值