2011-09-13 107 views
1

其實我陷在一個大problem..I已經創建了一個應用程序從中我可以在我的設備calendar..Now保存事件時,我在我的設備日曆中它總是會拯救我的應用程序的新事件刪除以前由我的應用程序保存的事件,並保存一個新的事件等..所有工作正常..現在最大的問題是,刪除它將刪除設備日曆中包含的日曆的所有事件,包括事件是由我的應用程序保存..所以我想要的是刪除只是由我的應用程序,而從我的應用程序插入新事件不是已經存在或哪些是我在設備日曆...直接分配的事件任何人都可以請幫我解決這個problem..the代碼我有插入和刪除是使用..日曆事件問題

Resources res = c.getResources(); 

    Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events"); 

    Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders"); 

    ContentResolver cr = c.getContentResolver(); 

    Uri uri= ContentUris.withAppendedId(EVENTS_URI, 1); 
    deleteEvent(cr, Resources res = c.getResources(); 

    Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events"); 

    Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders"); 

    ContentResolver cr = c.getContentResolver(); 

    //Deleting event from device calendar before saving new event 
    deleteEvent(cr, EVENTS_URI, 1); 

    //saving new data to calendar 
    ContentValues values = new ContentValues(); 
    values.put("calendar_id", 1); 
    values.put("title", str); 
    values.put("description", m_strDescription);      
    values.put("dtstart", cal.getTimeInMillis()); 
    values.put("dtend", cal.getTimeInMillis()); 
    values.put("hasAlarm", 1); 
     Uri event = cr.insert(EVENTS_URI, values); 

    values = new ContentValues(); 
    values.put("event_id", Long.parseLong(event.getLastPathSegment())); 
    values.put("method", 1); 
    values.put("minutes", 10); 
    cr.insert(REMINDERS_URI, values); 

功能刪除事件

private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) 
{ 
    Cursor cursor; 
    if (android.os.Build.VERSION.SDK_INT <= 7) 
    { 
     cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars_id=" + calendarId, null, null); 
    } 
    else 
    { 
     cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null); 
    } 
    while(cursor.moveToNext()) 
    { 
     long eventId = cursor.getLong(cursor.getColumnIndex("_id")); 
     resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null); 
    } 
    cursor.close(); 
} 

回答

2

您正在使用刪除每個事件的代碼:您需要保存您所創建的事件的ID,只刪除一個事件。當你這樣做:

cr.insert(REMINDERS_URI, values); 

的變化是這樣的:

Uri u = cr.insert(REMINDERS_URI, values); 

這將節省您創建事件的URI。然後,您可以將該URI傳遞到您的deleteEvent方法中,以僅刪除該事件,而不是所有事件。

+0

但我刪除功能隨時調用之前插入新event..i不知道如何處理,你可以請做到這一點對我code..please它真的會幫我 – AndroidDev

+0

這意味着,如果我寫的Uri U = CR。插入(REMINDERS_URI,價值觀),deleteEvent(CR,URI,值)..它給了我錯誤值不define.and這是因爲刪除函數被調用之前保存 – AndroidDev

+0

哎費米就可以普萊舍幫助我解決了這一點 – AndroidDev