Question

I have an application which uses the Calendar Provider for adding, editing and removing events. I have an interface of a calendar, which shows which days have events attached. Adding events works Perfectly. For adding an event I hardcoded the calendarID with the number 3, so calID = 3.

long calID=3;
values.put(Events.DESCRIPTION, description);
    values.put(Events.CALENDAR_ID, calID);
    values.put(Events.EVENT_TIMEZONE, "Europe/London");
    Uri uri = cr.insert(Events.CONTENT_URI, values);

This works perfectly. the View i've programmed shows the events. I open the Calendar application which comes included on my phone (Xperia J) and I can see that the event is there.

I tried to delete events with the following code:

        ContentResolver cr = mContext.getContentResolver();
    ContentValues values = new ContentValues();
    Uri deleteUri = null;
    deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
    int rows = mContext.getContentResolver().delete(deleteUri, null, null);

This code was DIRECTLY copied from developer.android.com website. This doesn't Delete! I debugged the code and the value of 'rows' is equal to 1, so effectivelly the row should have been deleted. However I get back to my app, and the event still exists there. Interesting thing: I've checked the Calendar Application that comes in my phone, and the event doesn't exist there. So it has been deleted (?). I thought that maybe i am not refreshing my app, so i closed it, reinstalled it, and it never deletes. I debug again the code for deletion, and once again the 'rows' value is equal to one, to the very same event! So, 'rows' is always returning 1 EVERY TIME i want to the delete it. So the row was deleted, but it was not deleted.

What is happening here? Is it because when I added the event, I hardcoded the calendarID?

Was it helpful?

Solution

As you noticed, deleting an event doesn't really remove the row from the DB until the event is synced to the server. It's quite logical, otherwise the calendar sync adapter would not be able to retrieve event data to send a DELETE request to the server. That's why there is a DELETED column in the event table, which you must check when querying events:

http://developer.android.com/reference/android/provider/CalendarContract.SyncColumns.html#DELETED

By the way if you query Instances instead of Events (which does expansion of recurring events), deleted events are removed automatically from the result.

OTHER TIPS

SOLVED. Basically the problem is that the event seemed to be locally removed on the phone, but was not yet synced with my Google Account. After syncing, the events were both removed from my phone and my Google Account associated to the phone.

This is another proof how annoying can be programming on Android, specially when special attention is not given to little details like these. I lost 2 days just to find out this basic issue...

There are two versions of delete: as an application and as a sync adapter. An application delete sets the deleted column to 1. This flag that tells the sync adapter that the row was deleted and that this deletion should be propagated to the server. A sync adapter delete removes the event from the database along with all its associated data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top