如何在android中的自定义列表视图中对列表项进行排序?

时间:2011-11-03 03:54:09

标签: java android listview

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

2 个答案:

答案 0 :(得分:4)

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

}

答案 1 :(得分:1)

我已经看过代码了,我在下面的语句中遇到了一些潜在的问题

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"))**;

此粗体语句将返回具有指定键的任何先前映射的值,如果没有此类映射,则返回null。所以我假设在这种情况下没有先前的映射。所以请确保您的阵列已填满