如何从Android中的Arraylist Date中降序排列日期?

问题描述 投票:11回答:6

我有一个ArrayList,其中存储着Date,并按降序对其进行了排序。现在,我想在ListView中显示它们。这是我到目前为止所做的:

 spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {
                case 0:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).lastModifiedDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 1:
                    list = DBAdpter.requestUserData(assosiatetoken);

                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat df3 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");    
                    String formattedDate3 = df3.format(c.getTime());                        
                    Log.v("log_tag", "Date  " + formattedDate3);

                    for (int i = 0; i < list.size(); i++) {                         
                        if (list.get(i).submitDate != null) {                               
                            String sDate = list.get(i).submitDate;                              
                            SimpleDateFormat df4 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");    
                            String formattedDate4 = df4.format(sDate);

                            Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>(){  
                                 public int compare(Date formattedDate3, Date formattedDate4) {
                                     return formattedDate3.compareTo(formattedDate4);
                                 }
                            });
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 2:

                    break;

                case 3:

                    break;

                default:
                    break;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }

        });
java android date arraylist sortedlist
6个回答
18
投票

创建Date类的Arraylist<Date>。并使用Collections.sort()升序。

请参见sort(List<T> list)

根据指定元素的自然顺序将指定列表升序排列。

对于降序排序,请参见Collections.reverseOrder()

Collections.sort(yourList, Collections.reverseOrder());

17
投票

仅在情况1中添加这样的内容:>

 case 0:
     list = DBAdpter.requestUserData(assosiatetoken);
     Collections.sort(list, byDate);
     for (int i = 0; i < list.size(); i++) {
         if (list.get(i).lastModifiedDate != null) {
             lv.setAdapter(new MyListAdapter(
                     getApplicationContext(), list));
         }
     }
     break;

并将此方法放在班级末尾

static final Comparator<All_Request_data_dto> byDate = new Comparator<All_Request_data_dto>() {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

    public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(ord1.lastModifiedDate);
            d2 = sdf.parse(ord2.lastModifiedDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return (d1.getTime() > d2.getTime() ? -1 : 1);     //descending
    //  return (d1.getTime() > d2.getTime() ? 1 : -1);     //ascending
    }
};

10
投票

您使用的日期的compareTo()将对升序有效。


9
投票

如果日期为字符串格式,则将其转换为每个对象的日期格式:


3
投票

DateDate,因此只需创建Comparable的列表并使用Comparable对其进行排序。并使用List<Date>获取Collections.sort()中的比较器。


0
投票

上述答案的更简单替代方法

© www.soinside.com 2019 - 2024. All rights reserved.