【发布时间】:2013-09-02 15:30:33
【问题描述】:
在我的 Android 应用程序中,我有一个 Appointment 对象列表,其中包含与约会相关的信息。然后,ListView 会填充这些约会的选择,按时间排序。
我已经为此列表视图编写了自己的自定义适配器,以便能够在约会之间有间隔的地方插入“空闲时间”约会。
这是我目前的代码:
ArrayList<Appointment> appointments = new ArrayList<Appointment>();
// populate arraylist here
ListIterator<Appointment> iter = appointments.listIterator();
DateTime lastEndTime = new DateTime();
int count = 0;
while (iter.hasNext()){
Appointment appt = iter.next();
lastEndTime = appt.endDateTime;
// Skips first iteration
if (count > 0)
{
if (lastEndTime.isAfter(appt.startDateTime))
{
if (iter.hasNext())
{
Appointment freeAppt = new Appointment();
freeAppt.isFreeTime = true;
freeAppt.subject = "Free slot";
freeAppt.startDateTime = lastEndTime;
freeAppt.endDateTime = lastEndTime.minusMinutes(-60); // Currently just set to 60 minutes until I solve the problem
iter.add(freeAppt);
}
}
}
count++;
}
DiaryAdapter adapter = new DiaryAdapter(this, R.layout.appointment_info, appointments);
我遇到的问题是逻辑问题。我一直在绞尽脑汁尝试寻找解决方案,但似乎我缺乏 Java 知识让我有点退缩了。
为了知道“空闲时间”约会何时结束,我必须知道下一个“真正”约会何时开始。但是直到迭代器的下一个周期,我才能获得该信息,此时“空闲时间”约会不再是上下文。
我该如何解决这个问题?
【问题讨论】: