gpt4 book ai didi

java - 如何在android中的自定义 ListView 中对列表项进行排序?

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:26 24 4
gpt4 key购买 nike

我正在尝试对列表项进行排序,我正在使用自定义 ListView 。在这里,我将项目存储在字符串数组中。但是在列出项目时, ListView 显示的是空项目而不是已排序的项目。但是在调试时,字符串数组以排序格式存储。如何解决?

最佳答案

 /**
* need to sort the ArrayList based on Person’s firstName.
* Here inside the Collections.sort method we are
* implementing the Comparator interface and overriding the compare method.
*/

Collections.sort(employeeList, new Comparator(){

public int compare(Object o1, Object o2) {
ContactInfo p1 = (ContactInfo) o1;
ContactInfo p2 = (ContactInfo) o2;
return p1.getEmployeeName().compareToIgnoreCase(p2.getEmployeeName());
}

});





this.fav_adapter = new FavoritesAdapter(this, R.layout.favorite_list_view, employeeList);
setListAdapter(this.fav_adapter);





public class ContactInfo {

private String employeeLpn;
private String employeeName;


/**
* Gets value for employeeLpn
* @return the employeeLpn
*/
public String getEmployeeLpn() {
return employeeLpn;
}
/**
* Sets the value for employeeLpn
* @param employeeLpn the employeeLpn to set
*/
public void setEmployeeLpn(String employeeLpn) {
this.employeeLpn = employeeLpn;
}
/**
* Gets value for employeeName
* @return the employeeName
*/
public String getEmployeeName() {
return employeeName;
}
/**
* Sets the value for employeeName
* @param employeeName the employeeName to set
*/
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

}

关于java - 如何在android中的自定义 ListView 中对列表项进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7990092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com