删除日历条目

发布于 2024-11-04 06:41:55 字数 659 浏览 1 评论 0原文

我编写了简单的代码来删除 Android 日历中的所有条目,但它没有删除任何内容。

源代码:

public void DeleteEvent(View view){

            int iNumRowsDeleted = 0;
            Uri eventsUri = Uri.parse("content://com.android.calendar/events");
            Cursor cur = getContentResolver().query(eventsUri, null, null, null, null);

            while (cur.moveToNext()){

                long id = cur.getLong(cur.getColumnIndex("_id"));
                Log.d(TAG, "ID: " + id);
                Uri eventUri = ContentUris.withAppendedId(eventsUri, id);
                iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
            }
        }

I wrote simple code for deleting all entries from android calendar,but it didn't delete nothing.

Source code:

public void DeleteEvent(View view){

            int iNumRowsDeleted = 0;
            Uri eventsUri = Uri.parse("content://com.android.calendar/events");
            Cursor cur = getContentResolver().query(eventsUri, null, null, null, null);

            while (cur.moveToNext()){

                long id = cur.getLong(cur.getColumnIndex("_id"));
                Log.d(TAG, "ID: " + id);
                Uri eventUri = ContentUris.withAppendedId(eventsUri, id);
                iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-11-11 06:41:56

我用它来删除:

private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) {
    Cursor cursor;
    if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1 
        cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars._id=" + calendarId, null, null);
    } else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
        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();
}

我用这样的方式调用它:

Uri eventsUri;
int osVersion = android.os.Build.VERSION.SDK_INT;
if (osVersion <= 7) { //up-to Android 2.1 
    eventsUri = Uri.parse("content://calendar/events");
} else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
    eventsUri = Uri.parse("content://com.android.calendar/events");
}
ContentResolver resolver = this.getContentResolver();
deleteEvent(resolver, eventsUri, calendarId);

I use this for delete:

private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) {
    Cursor cursor;
    if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1 
        cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars._id=" + calendarId, null, null);
    } else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
        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();
}

I call it with something like this:

Uri eventsUri;
int osVersion = android.os.Build.VERSION.SDK_INT;
if (osVersion <= 7) { //up-to Android 2.1 
    eventsUri = Uri.parse("content://calendar/events");
} else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
    eventsUri = Uri.parse("content://com.android.calendar/events");
}
ContentResolver resolver = this.getContentResolver();
deleteEvent(resolver, eventsUri, calendarId);
治碍 2024-11-11 06:41:56

使用此代码是正确的

        public void DeleteEvent(int your_event_id){

        int iNumRowsDeleted = 0;
        Uri eventsUri = Uri.parse("content://com.android.calendar/events");
        Cursor cur = getContentResolver().query(eventsUri, null, null, null, null);

        while (cur.moveToNext()){

             Uri eventUri = ContentUris.withAppendedId(eventsUri, your_event_id);
            iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
        }
    }

use this code its right

        public void DeleteEvent(int your_event_id){

        int iNumRowsDeleted = 0;
        Uri eventsUri = Uri.parse("content://com.android.calendar/events");
        Cursor cur = getContentResolver().query(eventsUri, null, null, null, null);

        while (cur.moveToNext()){

             Uri eventUri = ContentUris.withAppendedId(eventsUri, your_event_id);
            iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文