为了能够删除某些事件,您在获取事件列表时需要它的 _id。您可以通过将"_id" 添加到您的选择数组中来获得它。删除方法可能如下所示
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);
}
重要提示:你应该知道调用它并不会真正删除事件数据库记录,而只是将删除的列设置为1标记要删除的记录。这是因为同步适配器启用同步在服务器上删除。
知道这一点后,您应该编辑未删除的事件查询以不返回已删除的事件。示例如下:
private List<CalendarEntry> 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<CalendarEntry> calendarIds = new ArrayList<CalendarEntry>();
while (cursor.moveToNext())
{
int _id = cursor.getInt(0);
String title = cursor.getString(1);
calendarIds.add(new CalendarEntry(_id, title));
}
cursor.close();
return calendarIds;
}
您可以浏览列表并选择要删除的事件。
这里是列表的完整工作示例,并在项目单击时删除事件。
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<CalendarEntry> 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<CalendarEntry> calendarIds = new ArrayList<CalendarEntry>();
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<CalendarEntry>
{
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;
}
}
}