0

In my app I use two calendars, lets say calendar "business" with CalendarContract.Events.CALENDAR_ID = 1, and calendar "privat" with CalendarContract.Events.CALENDAR_ID = 2.

If the user chooses to see the events of both calendars in my app, I somehow need to get the events of both calendars into a cursor.

I have tried it like in the following code, where I put into my selection both calendarIds with an OR, but that shows me an incredible number of events.

So my question is, how to get a cursor with events of two calendars?

String[] projection = { CalendarContract.Events._ID,
            CalendarContract.Events.CALENDAR_ID,
            CalendarContract.Events.TITLE,
            CalendarContract.Events.DESCRIPTION,
            CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND,
            CalendarContract.Events.DURATION,
            CalendarContract.Events.DELETED };

SelectionArgs = new String[] { strCalendarStart,
                        strCalendarStop, "0",
                        intPrivateCalendarId.toString(),
                        intBeruflicheCalendarId.toString() };

selection = "((" + CalendarContract.Events.DTSTART + " >= ?) AND (" 
                        + CalendarContract.Events.DTEND + " <= ?)AND " + "("
                        + CalendarContract.Events.DELETED + " = ?) AND"
                        + "(" + CalendarContract.Events.CALENDAR_ID
                        + " = ?) OR" + "("
                        + CalendarContract.Events.CALENDAR_ID + " = ?))";

Cursor curCalendar = cr.query( CalendarContract.Events.CONTENT_URI,
                               projection, selection, SelectionArgs,
                               CalendarContract.Events.DTSTART + " ASC" );
trooper
  • 4,444
  • 5
  • 32
  • 32
roland
  • 227
  • 3
  • 11

1 Answers1

0

Have you tried adding parenthesis around the OR like this ?

selection = "((" + CalendarContract.Events.DTSTART + " >= ?) AND (" 
                        + CalendarContract.Events.DTEND + " <= ?)AND " + "("
                        + CalendarContract.Events.DELETED + " = ?) AND"
                        + "((" + CalendarContract.Events.CALENDAR_ID
                        + " = ?) OR" + "(" 
                        + CalendarContract.Events.CALENDAR_ID + " = ?)))";

Since I think OR's have a lower priority, your code would return events not deleted etc from calendar 1 and all events from calendar 2.

I am not completely sure but maybe worth trying. I hope this answer can help. Have a nice day.

minoru
  • 61
  • 1
  • 4