2016-07-04 5 views
0

イベントを追加してアンドロイドでプログラムで取得したいと考えています。カレンダーにイベントを追加するには2つの選択肢がありますが、どちらもイベントにIDを追加することはできません。私はIDの番号を32に設定しましたが、私はイベントを作成するときにIDが成長しています。次に、私が望むIDをどのように追加できますか?イベントIDの書き込みと読み取りAndroid

オプション1:

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

オプション2:

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

}

しかし、多分問題は、私はこれらのイベントを読み取ろうとする方法です。ここに私のコードです:

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

私は間違いをどこで行うのですか?誰かが考えを持っている場合は、応答してください。

答えて

0

データベース構造を投稿できますか?最初の考えの1つは、calendar_id列に自動インクリメントが有効になっていることです。その場合は、テーブルに別の列を追加したり、既存の列を変更したりできます。別の列を追加することをお勧めします。しかし、列テーブルの詳細を見てみましょう。

+0

申し訳ありませんが、どのような種類のデータベース構造を投稿するべきかわかりません。私がこれを行うために使用しているコードはすべて上にあります。自動インクリメントなしで別のIDを追加するにはどうすればいいですか? –

+0

カレンダーのDBを調べて、実際に必要な最大イベントIDを取得できるようです。ここに別のトピックがあります。質問をすることを躊躇しなければ、あなたを助けることができます[リンク] http://stackoverflow.com/questions/9482514/android-calendar-get-event-id –

関連する問題