【问题标题】:Android cardview place specific card at the end of the listAndroid cardview 将特定卡片放在列表末尾
【发布时间】:2019-04-26 14:22:14
【问题描述】:

我正在制作一个应用程序,其中用户输入任务显示在卡片视图中。每个任务都有自己的截止日期,当截止日期到时,它会显示在列表的底部。

在下面的代码中, 截止日期通过我的任务对象类传递。在将到期日期(.getTaskDate)与当前日期进行比较后,我应该将过期任务放在此处。

在我的适配器内部:

        //Overdue tasks
    String Currdate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());

    if(Currdate.compareTo(tasks.getTaskDate())>0 )
    {
        taskViewHolder.cardView.setBackgroundColor(mContext.getResources().getColor(R.color.colorRed));


    }

我应该如何实现这个功能,它是在 MainActivity 还是我的 Adapter 中完成的?我有类似的日期排序功能,可以在我的 MainActivity 中按升序对日期进行排序:

    private static void sortDates(final List<TaskObject> listViewItems) {
    Collections.sort(listViewItems, new Comparator<TaskObject>() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

        @Override
        public int compare(TaskObject t1, TaskObject t2) {

            try {
                return dateFormat.parse(t1.getTaskDate()).compareTo(dateFormat.parse(t2.getTaskDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return 0;
        }
    });
}

【问题讨论】:

  • 这段代码的输出是什么?
  • 您好,我添加了当前输出的屏幕截图。红牌(过期)应该在牌表的最后。

标签: java android sorting date calendar


【解决方案1】:

只需反转您的排序,您就完成了。将比较器中的return语句改为

return dateFormat.parse(t2.getTaskDate()).compareTo(dateFormat.parse(t1.getTaskDate()));

【讨论】:

  • 但我只希望Overdue任务出现在底部,其余按升序排序..这就是问题无法解决
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2016-07-03
  • 2020-06-09
相关资源
最近更新 更多