Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete or edit event from G-calendar from my android app?

I'm fetching a list of Google calendar events.

which event identifier can I get to be able to delete and edit specific event

Here is my fetching code:

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();
        }
    }

here I want to delete an event (by the way which type should I use for editing?)

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

//?
        startActivity(intent);
    }
like image 214
Elad Benda2 Avatar asked Oct 03 '22 03:10

Elad Benda2


1 Answers

To be able to delete some event you need its _id when fetching list of events. You can get it with adding "_id" to your selection array. Delete method could look like following

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);
}

Important: 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.

With knowing that, you should edit your not deleted event query to not return deleted events. Example follows:

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;
} 

That you can just browse the list and select the event to delete.

Here comes full working sample of list with deleting event on item click.

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;
        }
    }
}
like image 96
Josef Raška Avatar answered Oct 24 '22 00:10

Josef Raška