android developer tiny share-20170603

今天繼續講android日曆事件的ContentProvider,上一節講添加事件,這一節講更新和刪除事件。

以下是android developer官方講解:


更新事件


當您的應用想允許用戶編輯事件時,我們建議您按照使用 Intent 編輯事件中所述使用 EDIT Intent。不過,您可以在需要時直接編輯事件。 如需執行事件更新,您可以通過 URI 追加 ID (withAppendedId()) 或第一個選定項形式提供事件的 _ID。選定範圍應以 "_id=?" 開頭,並且第一個 selectionArg 應爲事件的 _ID。 您還可以使用不含 ID 的選定範圍執行更新。 以下是一個更新事件的示例。 它使用 withAppendedId() 方法更改事件的標題:

private static final String DEBUG_TAG = "MyActivity";
...
long eventID = 188;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
Uri updateUri = null;
// The new title for the event
values.put(Events.TITLE, "Kickboxing");
updateUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
int rows = getContentResolver().update(updateUri, values, null, null);
Log.i(DEBUG_TAG, "Rows updated: " + rows);  


刪除事件


您可以通過將事件 _ID 作爲 URI 追加 ID 或通過使用標準選定範圍來刪除事件。如果您使用追加 ID,則將無法同時使用選定範圍。共有兩個版本的刪除:應用刪除和同步適配器刪除。應用刪除將 deleted 列設置爲 1。此標誌告知同步適配器該行已刪除,並且應將此刪除操作傳播至服務器。 同步適配器刪除會將事件連同其所有關聯數據從數據庫中移除。 以下是一個應用通過事件 _ID 刪除事件的示例:

private static final String DEBUG_TAG = "MyActivity";
...
long eventID = 201;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
int rows = getContentResolver().delete(deleteUri, null, null);
Log.i(DEBUG_TAG, "Rows deleted: " + rows);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章