【问题标题】:How to compare Date and Boolean at same time in same compare and put with true booleans down and false upper in Android?如何在相同的比较中同时比较日期和布尔值,并在 Android 中使用真布尔值和假上值?
【发布时间】:2019-03-29 03:15:45
【问题描述】:

我正在创建事件应用程序,其中列出了所有事件。

我有一个问题,当活动结束时,它仍然保持不变,因为如果下一个活动是在一周之后,它会按时间排序。像这样:

所以,结束的事件,需要往下走,还要按日期排序。

我该怎么做?

我尝试按日期和布尔值分别排序,但失败了。 还是我需要为已结束的事件和即将到来的事件创建两个数组列表?

如果有人知道如何做到这一点,请告诉我,你拯救了我的一天。

我的代码,现在排序:

Collections.sort(eventsList, new Comparator<TimetableEvent>() {

                @Override
                public int compare(TimetableEvent arg0, TimetableEvent arg1) {
                    SimpleDateFormat format = new SimpleDateFormat(
                            "dd.MM.yyyy' klo 'HH.mm");
                    int compareResult = 0;
                    try {
                        Date arg0Date = format.parse(arg0.getDateTime());
                        Date arg1Date = format.parse(arg1.getDateTime());

                        compareResult = arg0Date.compareTo(arg1Date);




                    } catch (ParseException e) {
                        e.printStackTrace();
                        compareResult = arg0.getDateTime().compareTo(arg1.getDateTime());
                    }
                    return compareResult;
                }
            });

【问题讨论】:

    标签: android sorting arraylist


    【解决方案1】:

    只需调整您的 compare() 方法,这样,如果一个事件结束而另一个事件未结束,它会返回而不比较日期:

    @Override
    public int compare(TimetableEvent arg0, TimetableEvent arg1) {
        SimpleDateFormat format = new SimpleDateFormat(
                "dd.MM.yyyy' klo 'HH.mm");
        int compareResult = 0;
    
        if (arg0.isOver() && !arg1.isOver()) {
            return 1;
        }
        if (!arg0.isOver() && arg1.isOver()) {
            return -1;
        }
    
        try {
            Date arg0Date = format.parse(arg0.getDateTime());
            Date arg1Date = format.parse(arg1.getDateTime());
    
            compareResult = arg0Date.compareTo(arg1Date);
    
    
    
    
        } catch (ParseException e) {
            e.printStackTrace();
            compareResult = arg0.getDateTime().compareTo(arg1.getDateTime());
        }
        return compareResult;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      相关资源
      最近更新 更多