Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To be able to delete some event you need its <strong>_id</strong> when fetching list of events. You can get it with adding <code>"_id"</code> to your selection array. Delete method could look like following </p> <pre><code>private void deleteEvent(int eventId) { Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/events"); Uri uri = ContentUris.withAppendedId(CALENDAR_URI, eventId); getContentResolver().delete(uri, null, null); } </code></pre> <p><strong>Important:</strong> You should know that calling this does not really delete event database record, but only mark the record to delete with setting column deleted to 1. This is because of sync adapter to enable sync the deletion on server.</p> <p>With knowing that, you should edit your not deleted event query to not return deleted events. Example follows:</p> <pre><code>private List&lt;CalendarEntry&gt; readCalendar() { // Fetch a list of all calendars synced with the device, their title and _id // Notice that there is selection deleted = 0. Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), (new String[]{"_id", "title"}), "deleted = ?", new String[]{"0"}, null); List&lt;CalendarEntry&gt; calendarIds = new ArrayList&lt;CalendarEntry&gt;(); while (cursor.moveToNext()) { int _id = cursor.getInt(0); String title = cursor.getString(1); calendarIds.add(new CalendarEntry(_id, title)); } cursor.close(); return calendarIds; } </code></pre> <p>That you can just browse the list and select the event to delete.</p> <p>Here comes full working sample of list with deleting event on item click.</p> <pre><code>public class DeleteCalendarEventsActivity extends ListActivity { private TestAdapter mTestAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); mTestAdapter = new TestAdapter(this); setListAdapter(mTestAdapter); refreshList(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { int eventId = mTestAdapter.getItem(position).mId; deleteEvent(eventId); refreshList(); } private void deleteEvent(int eventId) { Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/events"); Uri uri = ContentUris.withAppendedId(CALENDAR_URI, eventId); getContentResolver().delete(uri, null, null); } private void refreshList() { mTestAdapter.clear(); for (CalendarEntry calendarEntry : readCalendar()) { mTestAdapter.add(calendarEntry); } } private List&lt;CalendarEntry&gt; readCalendar() { // Fetch a list of all calendars synced with the device, their title and _id Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), (new String[]{"_id", "title"}), "deleted = ?", new String[]{"0"}, null); List&lt;CalendarEntry&gt; calendarIds = new ArrayList&lt;CalendarEntry&gt;(); while (cursor.moveToNext()) { int _id = cursor.getInt(0); String title = cursor.getString(1); calendarIds.add(new CalendarEntry(_id, title)); } cursor.close(); return calendarIds; } static class TestAdapter extends ArrayAdapter&lt;CalendarEntry&gt; { TestAdapter(Context context) { super(context, 0); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null); } TextView textView = (TextView) convertView; textView.setText(getItem(position).mTitle); return convertView; } } static class CalendarEntry { private final int mId; private final String mTitle; CalendarEntry(int id, String title) { mId = id; mTitle = title; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload