在我的應用程序試圖通過按字母順序爲了在的ListView排序名稱 .GOT成功地從本地數據庫。可能值我知道什麼是實現我目標的正確方法?我沒有找到任何合適的解決方案。請幫助我。按字母順序排序類型的Hashmap值在Android中
我的代碼如下
// Declare this
ArrayList < HashMap < String, Object >> Results;
ArrayList < HashMap < String, Object >> ValuesSpeciality;
public CustomAdapterSpecialityActivity adapterCustom;
//get speciality list from local db and show on listview
public void SpecialitiesOnList() {
HashMap < String, Object > temp;
List <TaxonomyObjects> contacts = db.getAllTaxonomy();
for (TaxonomyObjects cn: contacts) {
temp = new HashMap < String, Object >();
temp.put("provider", "" + cn.getDisplayName());
temp.put("tvTaxonomyCode", "" + cn.getTaxonomyCode());
System.out.println(temp);
ValuesSpeciality.add(temp);
}
//searchResults=originalValuesSpeciality initially
Results = new ArrayList < HashMap < String, Object >> (ValuesSpeciality);
adapterCustom = new CustomAdapterSpecialityActivity(this, R.layout.list_item_service, searchResults);
lv.setAdapter(adapterCustom);
adapterCustom.notifyDataSetChanged();
for (int i = 0; i < adapterCustom.getCount(); i++) {
HashMap < String, Object > hm = (HashMap < String, Object >) adapterCustom.getItem(i);
hm.put("position", i);
}
}
結果我logcat中顯示了這種
{Code=204D00000X, provider=Neuromusculoskeletal Medicine & OMM}
{Code=204F00000X, provider=Transplant Surgery}
{Code=204R00000X, provider=Electrodiagnostic Medicine}
{Code=207K00000X, provider=Allergy & Immunology}
{Code=204E00000X, provider=Oral & Maxillofacial Surgery}
我試圖表明提供商名稱Alphabetically
秩序。
您不排序哈希映射。哈希映射的性質是未分類的 - 它使用哈希表,這是一種無序的數據類型。如果要對數據進行排序,請使用適當的數據結構,如列表或樹。 – 2015-02-10 07:29:49