如何按降序排列日期从Arraylist中的日期在Android? [英] How to Sort Date in descending order From Arraylist Date in android?

查看:245
本文介绍了如何按降序排列日期从Arraylist中的日期在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ArrayList which stores Dates and I sorted them in descending order. Now I want to display them in a ListView. This is what I did so far:

 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) {
            }

        });


推荐答案

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

请参阅 sort(List< T> list)


排序指定列表按照其元素的自然顺序升序。

Sorts the specified list into ascending order, according to the natural ordering of its elements.

按降序排序请参阅 Collections.reverseOrder()

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

这篇关于如何按降序排列日期从Arraylist中的日期在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆