【问题标题】:How to sort the list items in custom listview in android?如何在android的自定义列表视图中对列表项进行排序?
【发布时间】:2011-11-03 03:54:09
【问题描述】:

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

【问题讨论】:

  • 你能分享你的代码吗?
  • 我正在尝试对列表项进行排序是正确的还是您尝试存储的项目是正确的
  • 我正在尝试对已存储的列表项进行排序
  • PattabiRaman 尝试使用 Comparator 对项目进行排序

标签: java android listview


【解决方案1】:
 /**
    * 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;
    }

}

【讨论】:

    【解决方案2】:

    我已经看过代码,但下面的语句有一些潜在的问题

    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。所以我假设在这种情况下之前没有进行过映射。所以请确保您的数组已填满

    【讨论】:

      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2013-08-13
      • 1970-01-01
      • 2021-07-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多