日历中两个事件的不同时间



我一直在实现一个应用程序,并正在做一个带有函数的DAO类,该函数将返回当天的所有事件。但是,我有一个小虫子。例如:我们有两个事件-事件1(02:00-03:00(和事件2(14:00-16:00(。我们希望event1成为第一个。好吧,我们已经实现了一种排序,但是!事件2为1516024800000毫秒,事件1为1542618000000毫秒。

我知道给你一个工作样品会有帮助,但我不能…

这是一个功能:

public List<Schedule> getScheduleByDate(int year, int month, int day, String Account) {
List<Schedule> schedules = new ArrayList<>();
List<CalendarClass> calendarClasses = mCalendarClassDao.getTrueCalendars();
/*if(Account.equals("ANONYMOUS")){
return schedules;
}*/
String[] INSTANCE_PROJECTION = new String[]{
CalendarContract.Instances.CALENDAR_ID,      // 0
CalendarContract.Instances.TITLE,         // 1
CalendarContract.Instances.DESCRIPTION,
CalendarContract.Instances.DTSTART,
CalendarContract.Instances.DTEND,
CalendarContract.Instances.DISPLAY_COLOR,
CalendarContract.Instances.EVENT_COLOR,
CalendarContract.Instances.EVENT_COLOR_KEY,
CalendarContract.Instances.ALL_DAY,
CalendarContract.Instances.EVENT_LOCATION,
CalendarContract.Instances.OWNER_ACCOUNT,
CalendarContract.Instances.RRULE,
CalendarContract.Instances.ORIGINAL_INSTANCE_TIME
};
Calendar startTime = Calendar.getInstance();
startTime.set(year, month, day, 0, 0, 0);
long time = startTime.getTimeInMillis();
//time -= 1000;
//end fix
Calendar endTime = Calendar.getInstance();
endTime.set(year, month, day, 23, 59, 59);
long endMillis = endTime.getTimeInMillis();

for (int i = 0; i < calendarClasses.size(); ++i) {
//String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + time + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.CALENDAR_ID + " = " + "'" + calendarClasses.get(i).getId() + "'" + " ))";
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.READ_CALENDAR}, 1000);
}
String selection = CalendarContract.Instances.CALENDAR_ID + " = " + "'" + calendarClasses.get(i).getId() + "'";
//Cursor cursor = mContext.getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
// sort
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, time);
ContentUris.appendId(builder, endMillis);
Cursor cursor = mContext.getContentResolver().query(builder.build(),
INSTANCE_PROJECTION,
selection,
null,
CalendarContract.Instances.DTSTART);

while (cursor.moveToNext()){
Log.wtf("1", "1");
Schedule schedule = new Schedule();
schedule.setTitle(cursor.getString(1));
schedule.setDesc(cursor.getString(2));
schedule.setTime(cursor.getLong(3));
schedule.setTime_end(cursor.getLong(4));
schedule.setColor(cursor.getInt(5));
schedule.setLocation(cursor.getString(9));
schedule.setAccount(cursor.getString(10));
schedule.setRepeat(cursor.getString(11));
schedules.add(schedule);
}
cursor.close();
}
Log.wtf("wtf", String.valueOf(schedules.size()));

if (schedules.size() > 1) {
int i = 0;
int goodPairsCounter = 0;
while (true) {
long time1 = schedules.get(i).getTime();
long time2 = schedules.get(i + 1).getTime();
Log.wtf("TIME", String.valueOf(time1) + " - " + String.valueOf(time2));
Log.wtf("TIME", schedules.get(i).getTitle() + " - " + schedules.get(i+1).getTitle());
if (time1 > time2) {
Log.wtf("TIME", "hop");
Schedule sh = new Schedule(schedules.get(i).getId(), schedules.get(i).getColor(), schedules.get(i).getTitle(), schedules.get(i).getDesc(), schedules.get(i).getLocation(), schedules.get(i).getState(), schedules.get(i).getTime(), schedules.get(i).getTime_end(), schedules.get(i).getYear(), schedules.get(i).getRepeat(), schedules.get(i).getAccount());
schedules.remove(i);
schedules.add(i + 1, sh);
goodPairsCounter = 0;
} else {
goodPairsCounter++;
}
i++;
if (i == schedules.size() - 1) {
i = 0;
}
if (goodPairsCounter == schedules.size() - 1) break;
}
}
return schedules;
}

这是的屏幕截图

需要知道如何解决这个问题

关闭。还没有解决。如果你被卡住了,就去找另一种方法

最新更新