简体   繁体   中英

How to sort the list items in custom listview in android?

I'm trying to sort the list items, I'm using custom listview. In this i have stored the items in the string array. but while listing the items, the listview is showing empty items instead of sorted items. But while debugging the string arrays are stored in sorted format. How to solve it?

 /**
    * 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;
    }

}

I have seen the code and I got some potential problem with below statement

HashMap<String, String> sampleObjectMap = new HashMap<String, String>();

            titles[i-1]=**sampleObjectMap.put("title", dh.val1(i-1))**;
            persons[i-1]=**sampleObjectMap.put("person", dh.pers(i-1))**;
            priorities[i-1]=**sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.getpriority(i-1))))**;
            dates[i-1]=**sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i-1)),"dd/MM/yyyy"))**;

This bold statement will return the value of any previous mapping with the specified key or null if there was no such mapping. so I am assuming there were no previous mapping has been done in this case. so please make sure you array is filled up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM