[英]Sorting using SimpleAdapter
我想根据我的应用程序中的某些值对Listview
进行排序,但是我不知道如何对其进行编码。 我研究发现,人们正在使用比较器和集合对数组列表进行排序。 基本上,我想按某些状态排序,例如接受或拒绝。 该值存储在TAG_ACCEPT_REJECT
并显示在R.id.applicantStatus
。
下面是我的代码:
adapter = new SimpleAdapter(
ApplicantJobStatusActivity.this,
applicantsList,
R.layout.list_applicant_status,
new String[] { TAG_UID, TAG_NAME,
TAG_ACCEPT_REJECT,
TAG_ACCEPT_REJECT_DATETIME },
new int[] { R.id.applicantUid,
R.id.applicantName,
R.id.applicantStatus,
R.id.accept_reject_datetime });
setListAdapter(adapter);
我想知道如何以及在何处使用比较器和集合来放置排序代码以重新排列ListView
。
您应先对列表的“ applicantsList
列表”进行排序,然后再将其传递给适配器。 然后,您可以使用Collections.sort
方法。 像这样:
Collections.sort(applicantsList, new Comparator<T extends Map<String, ?>> {
public int compare(T map1, T map2) {
if (!map1.has(TAG_ACCEPT_REJECT)) return -1;
if (!map2.has(TAG_ACCEPT_REJECT)) return 1;
// Assumes the objects you store in the map are comparables
return map1.get(TAG_ACCEPT_REJECT).compareTo(map2.get(TAG_ACCEPT_REJECT));
}
});
- 在将Array
分配给Adapter
之前使用Arrays.sort()
。
-或者您可以将Comparable
Interfaces与Collections.sort(Collection c)
一起使用,
-或具有Comparator Interface, with Colletions like
Collections.sort(Collection c, Comparator cp)
Comparator Interface, with Colletions like
before assigning it to the
Comparator Interface, with Colletions like
before assigning it to the
Comparator Interface, with Colletions like
ArrayList before assigning it to the
Collet。
/////////////////////////// Edited Part ///////////////////////// ///
请参阅下面的链接,其中包含使用各种技术对Array,ArrayList等进行排序的所有示例。
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.