如何从我的Android应用程序中删除或编辑G-calendar中的事件?

时间:2013-12-15 07:27:29

标签: java android android-intent calendar google-calendar-api

我正在提取Google日历活动列表。

我可以使用哪个事件标识符来删除和编辑特定事件

这是我的提取代码:

private void fetchEvents() {
        String[] selection = new String[] { "calendar_id", "title", "description",
                "dtstart", "dtend", "eventLocation" };

        String projection = "description LIKE ?";
        String[] selecionArgs = new String[]{"%/images/%"};
        String orderby = "dtstart ASC";

        Cursor cursor = getContentResolver()
                .query(
                        Uri.parse("content://com.android.calendar/events"),
                        selection, projection,
                        selecionArgs, orderby);
        cursor.moveToFirst();
        // fetching calendars name
        String CNames[] = new String[cursor.getCount()];

        for (int i = 0; i < CNames.length; i++) {
            nameOfEvent.add(cursor.getString(1));
            cursor.moveToNext();
        }
    }

这里我想删除一个事件(通过我应该使用哪种类型进行编辑?)

    private void deleteCalendarEvent_intent() {
        Intent intent = new Intent(Intent.ACTION_DELETE)

//?
        startActivity(intent);
    }

2 个答案:

答案 0 :(得分:4)

为了能够删除某些事件,您需要在获取事件列表时使用 _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;
        }
    }
}

答案 1 :(得分:0)

试试this library。它负责映射日历事件,您可以使用对象方法执行CRUD操作。