0

I'd like to add an event and get it back programmatically in android. I have two option to add an event to the calender but neither of them good at adding ID to the event. I set the number of the ID into 32 but when I create an event it's ID is growing up. Then how can I add the ID I want?

Option1:

public void InsertAnEvent2(){
    Calendar calendarEvent = Calendar.getInstance();
    Intent i = new Intent(Intent.ACTION_EDIT);
    i.setType("vnd.android.cursor.item/event");
    i.putExtra("beginTime", calendarEvent.getTimeInMillis());
    i.putExtra("allDay", true);
    i.putExtra("rule", "FREQ=YEARLY");
    i.putExtra("endTime", calendarEvent.getTimeInMillis() + 60 * 60 * 1000);
    i.putExtra("title", "Eskuvo");
    i.putExtra("calendar_id",32);
    startActivity(i);
}

Option2:

 public void Mindencalendar(){
    ContentResolver cr = getActivity().getContentResolver();
    Calendar calendarEvent = Calendar.getInstance();
    Log.d("i'm","here1");
    long idk[] = new long[10];
    idk[0] = cu.addEventToCalender(cr,"a","b","c",5,calendarEvent.getTimeInMillis());
    Log.d("id","id"+idk[0]);
}

public class CalendarUtils {

public static long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
                                      long startDate) {

    String eventUriStr = "content://com.android.calendar/events";
    ContentValues event = new ContentValues();
    event.put("calendar_id", 32);
    event.put("title", title);
    event.put("description", addInfo);
    event.put("eventLocation", place);
    event.put("eventTimezone", "UTC/GMT +2:00");

    // For next 1hr
    long endDate = startDate + 1000 * 60 * 60;
    event.put("dtstart", startDate);
    event.put("dtend", endDate);
    //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true
    // values.put("allDay", 1);
    event.put("eventStatus", status);
    event.put("hasAlarm", 1);

    Uri eventUri = cr.insert(Uri.parse(eventUriStr), event);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

    return eventID;
}

}

But maybe the problem is how I try to read these events. Here is my code:

 public void ReadFromCalendar(){

    Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
    ContentResolver cr = getActivity().getContentResolver();
    Cursor cursor;
    cursor = cr.query(EVENTS_URI, null, null, null, null);
    //int a = cursor.getCount();
    while(cursor.moveToNext()) {
        long id = cursor.getLong(cursor.getColumnIndex("calendar_id"));
        Log.d("TAG", "ID: " + id);
        Uri eventUri = ContentUris.withAppendedId(EVENTS_URI, id);
    }
    cursor.close();
}

I don't know where do I make a mistake. If anyone has an idea please response.

T.dog
  • 91
  • 1
  • 12

1 Answers1

0

Can you post the database structure, one of the first thoughts was that your calendar_id column has the auto-increment enabled. In that case you can add another column to your table or alter the existing one, my recommendation is to add another one. But let's see the columns table details.

Alin Stelian
  • 861
  • 1
  • 6
  • 16
  • Sorry but I have no idea what kind of database structure should I post . All code I'm using to do this is there above. Can you tell me then, how can I add another id without auto-increment enabled? – T.dog Jul 04 '16 at 08:33
  • It looks like you can interrogate the calendar db, and get max event id, which is what you really need. Here is another topic, which can help you, if have questions don't hesitate to ask them, [link] http://stackoverflow.com/questions/9482514/android-calendar-get-event-id – Alin Stelian Jul 04 '16 at 11:51