0

I'm making an app where user-input tasks are shown in cardview. Each tasks have their own due dates and when the due date is up, it will show up at the bottom of the list.

In the code below, The due dates are passed via my task object class. After comparing due dates (.getTaskDate) with current date, I should put the overdue task at the bottom here.

Inside my adapter:

        //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));


    }

How should I implement this feature and is it done inside the MainActivity or my Adapter? I have similar date sorting feature which sorts the date in ascending order inside my 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;
        }
    });
}

enter image description here

2
  • What is the output with this code? Commented Nov 24, 2018 at 6:46
  • Hi, I have added a screenshot of my current output. The red card (Overdue) should be at the end of the card list. Commented Nov 24, 2018 at 6:54

1 Answer 1

0

Just reverse your sorting and you are done. Change the return statement in the comparator to

return dateFormat.parse(t2.getTaskDate()).compareTo(dateFormat.parse(t1.getTaskDate()));
Sign up to request clarification or add additional context in comments.

1 Comment

but I only wanted the Overdue task to appear at the bottom, the rest are sorted in ascending order.. that's the problem couldn't figure out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.