如何将数据库中的事件添加到android日历
我正在将数据库中的事件添加到日历(Etar日历)。我尝试使用如何将数据库中的事件添加到android日历,android,calendar,Android,Calendar,我正在将数据库中的事件添加到日历(Etar日历)。我尝试使用insert(Events.CALENDAR\u URI,values)加载它 我在数据库中有很多天的事件,在将事件加载到日历后,它会显示3天,在完成循环语句后,第3天的事件将消失,并且在接下来的几天内它也不会显示事件 dbHelper = new DatabaseHelper(this); SQLiteDatabase database = dbHelper.getWritableDatabase(); Cursor curso
insert(Events.CALENDAR\u 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();
如何解决这个问题?得到了答案,我将数据库中的“id”传递到CALENDAR\u id
values.put(CalendarContract.Events.CALENDAR\u id,id)代码>。我们必须将日历ID作为1传递