我收到了Json的回复,需要按照有序名称显示结果。在实现Hashmap后,我意识到它没有按字母顺序排序我该怎么做?
public ArrayList<HashMap<String, String>> contactList= new ArrayList<HashMap<String, String>>();
Collections.sort(contactList, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
代码:
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
SimpleAdapter adapter = new SimpleAdapter(
HomeActivity.this, contactList,
R.layout.list_item, new String[] {TAG_FNAME}, new int[] {R.id.textView1});
setListAdapter(adapter);
adapter.notifyDataSetChanged();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position, long ids) {
Intent i = new Intent(getApplicationContext(),
SingleListActivity.class);
HashMap<String, String> contact = contactList.get(position);
i.putExtra("id", id);
i.putExtra("firstName", firstname);
i.putExtra("lastName", lastname);
i.putExtra("headline", headline);
i.putExtra("pictureUrl", pictureUrl);
i.putExtra("url", url);
startActivity(i);
}
});
在这里它包含名字和姓氏我该怎么做?
答案 0 :(得分:0)
要对HashMap的ArrayList进行排序,请使用此代码。
Collections.sort(ArrayList,new Comparator<HashMap<String,String>>(){
public int compare(HashMap<String,String> mapping1,HashMap<String,String> mapping2){
return mapping1.get("Name").compareTo(mapping2.get("Name"));
}
在大多数情况下,HashMaps具有相同的密钥,因此您可以使用此代码。(将“名称”更改为您的密钥)。将ArrayList替换为你的Arraylist名称,contactList。
希望这会有所帮助:)
答案 1 :(得分:-1)
如果您检查“如何对每个持有多个键值对的HashMaps的ArrayList进行排序”,那么已经讨论过,请查看stackoverflow.com/questions/5369573
如果您检查“android-listview-display-as-alphabetic-order”,请查看此stackoverflow讨论stackoverflow.com/questions/12559843