2017-07-11 16 views
0

データベースからカレンダー(Etar-Calendar)にイベントを追加しています。私はinsert(Events.CALENDAR_URI, values);を使ってロードしようとしました。データベースからアンドロイドカレンダーにイベントを追加する方法

データベースに何日もイベントがあり、3日間表示されたカレンダーにイベントを読み込んだ後、3日目のループステートメントイベントが終了し、さらにイベントが表示されなくなります。

dbHelper = new DatabaseHelper(this); 
SQLiteDatabase database = dbHelper.getWritableDatabase(); 

Cursor cursor = database.rawQuery("select * from events", null); 

SimpleDateFormat startFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.getDefault()); 
SimpleDateFormat endFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.getDefault()); 

if (cursor.moveToFirst()) { 
    do { 
     id = cursor.getString(cursor.getColumnIndexOrThrow("id")); 

     name = cursor.getString(cursor.getColumnIndexOrThrow("name")); 

     date = cursor.getString(cursor.getColumnIndexOrThrow("date")); 

     start_time = cursor.getString(cursor.getColumnIndexOrThrow("start_time")); 

     end_time = cursor.getString(cursor.getColumnIndexOrThrow("end_time")); 

     description = cursor.getString(cursor.getColumnIndexOrThrow("description")); 

     location = cursor.getString(cursor.getColumnIndexOrThrow("location")); 

     try { 
      start = date + " " + start_time; 
      end = date + " " + end_time; 

      eventStart = startFormat.parse(start); 
      eventEnd = endFormat.parse(end); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     ContentValues values = new ContentValues(); 
     values.put(CalendarContract.Events.CALENDAR_ID, id); 
     Log.e("MonthActivity", "eventId " + eventId); 
     values.put(CalendarContract.Events.TITLE, name); 
     Log.e("MonthActivity", "eventName " + name); 
     values.put(CalendarContract.Events.DTSTART, eventStart.getTime()); 
     values.put(CalendarContract.Events.DTEND, eventEnd.getTime()); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getTimeZone("Asia/Calcutta"))); 

     checkAppPermissions(); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 

      return; 
     } 
     context.getApplicationContext().getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values); 

    } while (cursor.moveToNext()); 
} 
cursor.close(); 

解決方法

答えて

0

は答えを得た、私はCALENDAR_IDvalues.put(CalendarContract.Events.CALENDAR_ID, id);にデータベース内にある「ID」を渡しました。 CALENDAR_IDを1にする必要があります。

関連する問題