我想根据我的应用程序中的一些值对我的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。
发布于 2012-10-22 13:18:48
在将列表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));
}
});发布于 2012-10-22 13:25:03
-在将Array分配给Adapter之前使用Arrays.sort() 。
-或者您可以使用Collections.sort(Collection c)与Comparable接口,
-或Collections.sort(Collection c, Comparator cp)与Comparator Interface, with Colletions like ArrayListbefore assigning it to the适配器。
Part////////////////////////// /编辑的Part//////////////////////////
请参阅下面的链接,该链接包含了使用各种技术对数组、等进行排序的所有示例。
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
https://stackoverflow.com/questions/13012282
复制相似问题