Hi Giles, The code below will add an event every time onClick is called since it's doing an insert(). If you want to modify an existing event, you need to get the event id via a query then do an update. Search for "Updating Events" in http://developer.android.com/guide/topics/providers/calendar-provider.html
Thanks, Mike On Tue, Mar 13, 2012 at 7:28 AM, giles ian <[email protected]> wrote: > Hi, > > I am using the below code for adding events. > > Now the issue is im able to add same event (all details same) multiple > times. > > How to i avoid this. > > >>> >>> public static void addToCalendar(Context ctx, final String title, >>> >>> final long dtstart, final long dtend) { >>> >>> final ContentResolver cr = ctx.getContentResolver(); >>> >>> Cursor cursor; >>> >>> if (Integer.parseInt(Build.VERSION.SDK) >= 8) >>> >>> cursor = cr.query( >>> >>> Uri.parse("content://com.android.calendar/calendars"), >>> >>> new String[] { "_id", "displayname" }, null, null, null); >>> >>> else >>> >>> cursor = cr.query(Uri.parse("content://calendar/calendars"), >>> >>> new String[] { "_id", "displayname" }, null, null, null); >>> >>> if (cursor.moveToFirst()) { >>> >>> final String[] calNames = new String[cursor.getCount()]; >>> >>> final int[] calIds = new int[cursor.getCount()]; >>> >>> for (int i = 0; i < calNames.length; i++) { >>> >>> calIds[i] = cursor.getInt(0); >>> >>> calNames[i] = cursor.getString(1); >>> >>> cursor.moveToNext(); >>> >>> } >>> >>> >>> AlertDialog.Builder builder = new AlertDialog.Builder(ctx); >>> >>> builder.setSingleChoiceItems(calNames, -1, >>> >>> new DialogInterface.OnClickListener() { >>> >>> >>> @Override >>> >>> public void onClick(DialogInterface dialog, int which) { >>> >>> ContentValues cv = new ContentValues(); >>> >>> cv.put("calendar_id", calIds[which]); >>> >>> cv.put("title", title); >>> >>> cv.put("dtstart", dtstart); >>> >>> cv.put("hasAlarm", 1); >>> >>> cv.put("dtend", dtend); >>> >>> >>> Uri newEvent; >>> >>> if (Integer.parseInt(Build.VERSION.SDK) >= 8) >>> >>> newEvent = cr.insert( >>> >>> Uri.parse("content://com.android.calendar/events"), >>> >>> cv); >>> >>> else >>> >>> newEvent = cr.insert( >>> >>> Uri.parse("content://calendar/events"), >>> >>> cv); >>> >>> >>> if (newEvent != null) { >>> >>> long id = Long.parseLong(newEvent >>> >>> .getLastPathSegment()); >>> >>> ContentValues values = new ContentValues(); >>> >>> values.put("event_id", id); >>> >>> values.put("method", 1); >>> >>> values.put("minutes", 0); // 15 minuti >>> >>> if (Integer.parseInt(Build.VERSION.SDK) >= 8) >>> >>> cr.insert( >>> >>> Uri.parse("content://com.android.calendar/reminders"), >>> >>> values); >>> >>> else >>> >>> cr.insert( >>> >>> Uri.parse("content://calendar/reminders"), >>> >>> values); >>> >>> >>> } >>> >>> dialog.cancel(); >>> >>> } >>> >>> >>> }); >>> >>> >>> builder.create().show(); >>> >>> } >>> >>> cursor.close(); >>> >>> } > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

