2014-09-02 27 views
4

我想更新日曆事件。如何從Android日曆事件中刪除參加者?

我知道如何更新標題,位置,使用ContentResolver

添加新的與會者,但我不知道如何通過電子郵件刪除一些與會者,例如。

這是我到目前爲止,我解析JSONObject和獲取所有新信息:

public void updateEvent(Context context, long eventId, JSONObject updateObj) { 
     ContentResolver cr = context.getContentResolver(); 
     ContentValues values = new ContentValues(); 
     Uri updateUri = null; 

     long startDate = updateObj.optLong("startDate"); 
     long endDate = updateObj.optLong("endDate"); 
     String title = updateObj.optString("title"); 
     String description = updateObj.optString("description"); 
     String location = updateObj.optString("location"); 
     int eventStatus = updateObj.optInt("eventStatus"); 
     JSONArray addAtt = updateObj.optJSONArray("add_attendee"); 
     JSONArray deleteAtt = updateObj.optJSONArray("delete_attendee"); 

     values.put(Events.EVENT_LOCATION, location); 
     values.put(Events.DESCRIPTION, title); 
     values.put(Events.TITLE, title); 
     values.put(Events.DTSTART, startDate); 
     values.put(Events.DTEND, endDate); 
     values.put(Events.STATUS, eventStatus); 

     updateUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); 
     int rows = cr.update(updateUri, values, null, null); 
     Log.i(TAG, "Rows updated: " + rows); 

     // add new attendees 
     for(int i=0; i<addAtt.length(); i++){   

      JSONObject attObj = addAtt.optJSONObject(i); 

      String name = attObj.optString("name"); 
      String email = attObj.optString("email");   
      int relationship = attObj.optInt("relationship"); 
      int type = attObj.optInt("type"); 
      int status = attObj.optInt("status"); 

      values = new ContentValues(); 
      values.put(Attendees.ATTENDEE_NAME, name); 
      values.put(Attendees.ATTENDEE_EMAIL, email); 
      values.put(Attendees.ATTENDEE_RELATIONSHIP, relationship); // had 0 
      values.put(Attendees.ATTENDEE_TYPE, type);// had 0 
      values.put(Attendees.ATTENDEE_STATUS, status); // had 3 - invited 
      values.put(Attendees.EVENT_ID, eventId); 
      updateUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, eventId); 
      cr.update(updateUri, values, null, null); 
     } 

     // remove attendees 
     for(int i=0; i<removeAtt.length(); i++){    

      JSONObject attObj = addAtt.optJSONObject(i); 

        // HERE iS A PROBLEM 

//   Uri deleteUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, eventId); 
//   int rows = cr.delete(deleteUri, null, null); 
     } 
    } 

[編輯]

我也試過:

String selection = Attendees.ATTENDEE_EMAIL + " = ?"; 
String[] selectionArgs = new String[] {"[email protected]"}; 

Uri deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); 
rows = cr.delete(deleteUri, selection, selectionArgs); 

得到錯誤:

java.lang.IllegalArgumentException: Selection not permitted for content://com.android.calendar/events/524 

請幫幫忙,

+0

,如果你有一個新與會者更新,是否追加或更換與會者名單? – ataulm 2014-09-02 09:06:19

+0

@ataulm它增加了一個新的存在列表 – snaggs 2014-09-02 09:17:31

+0

還有一個Attendees.CONTENT_URI,你可以使用它在正確的表中執行刪除? – ataulm 2014-09-02 09:24:53

回答

2

要刪除的電子郵件和事件ID使用特定的與會者:

String selection = "(" + Attendees.EVENT_ID + " = ?) AND (" + Attendees.ATTENDEE_EMAIL + " = ?)"; 
    String[] selectionArgs = new String[] {eventId+"","[email protected]"}; 

    rows = cr.delete(Attendees.CONTENT_URI, selection, selectionArgs); 
    Log.i(TAG, "Rows updated: " + rows); 
+1

這將刪除事件本身。 – ataulm 2014-09-02 09:04:46

+0

嗨,我想用這種方法,但它dosent刪除與會者返回行數爲零 – 2014-10-27 18:05:41

+0

檢查您的代碼。你得到的是 – RajeshVijayakumar 2014-10-28 06:05:03

-1

你可以delete日曆事件,並添加與插入一個新的(沒有你希望刪除這一與會者)。


你能不能使用delete方法,用Attendees.CONTENT_URI刪除從您希望的事件的參與者?

http://developer.android.com/guide/topics/providers/calendar-provider.html#attendees

String emailAddress = ...; 
cr.delete(Attendees.URI, Attendees.ATTENDEE_EMAIL + "=" + emailAddress, null); 
+0

你必須指向特定的事件ID才能刪除與會者 – snaggs 2014-09-02 09:32:33

+0

但你有事件ID .. – ataulm 2014-09-02 11:02:45