Question

I am trying to return a query of Calendar events sorted by date and time so that I can iterate through them. I have queried it like so:

private Cursor mCursor = null;
private static final String[] COLS = new String[]{CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.AVAILABILITY};
mCursor = getContentResolver().query(CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
mCursor.moveToLast();

This works great except it seems to return the calendar events based on when it was created and not by date and time. For example, if I create a new event for December 25, 2012, it will show up as the last result.

My end goal is that I would like to find the first event that starts before the current time. For example, if it is currently 8:00am, I want to find the first event that starts before 8:00am. I want to then check the duration of that event to see if it is a currently ongoing event.

Is there an easy way to return a query sorted by date and time or will I have to implement a sort after the results are queried? Better yet, is there a simple way to find the first event before the current time?

Was it helpful?

Solution

maybe this?

    private Cursor mCursor = null;
    private static final String[] COLS = new String[]{CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.AVAILABILITY};
    mCursor = getContentResolver().query(CalendarContract.Events.CONTENT_URI, COLS, null, null, CalendarContract.Events.DTSTART + " ASC");
    mCursor.moveToLast();

OTHER TIPS

another way

Collections.sort(dateList, new Comparator<Date>(){
           public int compare(Date date1, Date date2){
          return date1.after(date2);
        }
      });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top