Search code examples
javaandroiddatelistviewcalendar

Show only To-dos with today's due-date in Android app


For my app I need a function that only shows the to-dos which due date is today. I am new to Android Studio and Java, so unfortunately I do not know how to write this code.

This is my code so far:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getIdemid()) {
        case R.id.menu_dailyToDo:
            showDailyTodo();
            adapter.notifyDataSetChanged();
            return true;
       }
    }
}

public void showDailyTodo() {
    if(tvDueDate == //today) {
        // code that listview items with duedate today are shown.
    }
}

All I could find so far could not help me.

Edit

In the picture you can see my listview.

enter image description here

The date is a TextView with ID tvDueDate.


Solution

  • You can use System.currentTimeMillis() to get current time upto milliseconds as long datatype.

    OR

    You can use Date d = new Date(); to get the date as a Date object and convert it to String in desirable format like

    Date d = new Date();
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    String formattedDate = df.format(d);
    

    It depends on datatype and format in which you have stored dates in tvDueDate.

    Then you can set Todos Items into a ListView adapter as shown in this post Custom Adapter for List View

    EDIT

    you can get your due date in a String using

    String dueDate = tvDueDate.getText().toString();
    

    Then do

    public void showDailyTodo() {
    Date d = new Date();
    SimpleDateFormat df = new SimpleDateFormat("dd.mm.yyyy");
    String formattedDate = df.format(d);
    if(dueDate.equals(formattedDate)) {
        // code that listview items with duedate today are shown.
    }
    }