你好Android和Android的主人Java :)我今天花了大约5个小时试图找出如何过滤我的ArrayList<WeekViewEvent>
(我正在使用Alamkanak的WeekView外部库)所以我的自定义arrayadapter布局可以显示与今天的日期匹配的事件(仅限星期一) 2月5日星期一的活动,2月6日星期二的星期二活动,等等......)。
这是我创建WeekViewEvent
的ArrayList:
List<WeekViewEvent> newEvents = new ArrayList<WeekViewEvent>();
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 12);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, 1);
startTime.set(Calendar.YEAR, 2018);
Calendar endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR, 13);
endTime.set(Calendar.MINUTE, 0);
endTime.set(Calendar.MONTH, 1);
endTime.set(Calendar.YEAR, 2018);
WeekViewEvent event = new WeekViewEvent(7777, "All lucky 7's, startTime, endTime);
event.setColor(R.color.event_color_01);
newEvents.add(event);
然后将列表传递给适配器:
Adapter adapterevent = new Adapter(this, events);
ListView listView = (ListView) findViewById(R.id.harmonogram_list_view);
listView.setAdapter(adapterevent);
甚至可以按日期“过滤”事件吗?正在阅读其他一些帖子,但说实话,我使用ArrayList执行的唯一操作仅限于添加,删除或获取数组...
我正在考虑的是通过循环和搜索开始和结束日期来过滤事件,然后创建新的ArrayList,如:
ArrayList<WeekViewEvent> filteredEvents = new ArrayList<>();
for (WeekviewEvent event : NewEvents) {
if(event.getStartTime().getTimeInMillis() >= start of the current day && event.getEndTime().getTimeInMillis <= end of the current day
events.add(event);
}
}
return filteredEvents;
但我不确定这是否是正确的方法,如果是这样的话 - 如何编写其余的代码来抓取所选的事件。
任何帮助将不胜感激!先谢谢你,马特:)
答案 0 :(得分:0)
最后,通过获取/设置Calendar
的实例,将日期转换为毫秒并使循环变形(我认为),我能够使其工作:
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 0);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
Calendar endTime = (Calendar) startOfMonth.clone();
endTime.set(Calendar.HOUR_OF_DAY, 23);
endTime.set(Calendar.MINUTE, 59);
endTime.set(Calendar.SECOND, 59);
ArrayList<WeekViewEvent> events = new ArrayList<>();
for (WeekViewEvent event : newEvents) {
if (event.getEndTime().getTimeInMillis() > startTime.getTimeInMillis() &&
event.getStartTime().getTimeInMillis() < endTime.getTimeInMillis())
{
events.add(event);
}
}
现在,适配器显示符合日历条件(实际日期相同)的事件。