我的班级Event
有一堆不同的变量,如名称、描述、开始时间和结束时间,然后我有Day
班级有可变日期和ArrayList
这些Events
。现在我想要 ArrayList Days
,并且每个都Day
应该包含Events
在同一天。默认情况下,我得到事件列表,我需要根据这个列表创建日期。所有时间都是int unix timestamp
格式。
澄清我所追求的示例:
Day 25.11
Event1
Event2
Event3
Day 26.11
Event1
Day 27.11
Event1
Event2
Event3
Event4
我尝试了没有工作结果的代码:
private void getObjects(String url) throws JSONException, Exception {
JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get());
JSONArray job1 = jsonObject.getJSONArray("events");
ArrayList<Event> events = new ArrayList<Event>();
Calendar calPrev = Calendar.getInstance();
boolean first = true;
for (int i = 0; i < job1.length(); i++) {
JSONObject myEventArray = job1.getJSONObject(i);
int start = myEventArray.getInt("start");
int end = myEventArray.getInt("end");
String name = myEventArray.getString("name");
String description = myEventArray.getString("description");
if(first){
calPrev.setTimeInMillis(start*1000L);
events.add(new Event(start,end,name,description));
first = false;
}else{
Calendar calCur = Calendar.getInstance();
calCur.setTimeInMillis(start*1000L);
if(calPrev.get(Calendar.YEAR) == calCur.get(Calendar.YEAR) && calPrev.get(Calendar.DAY_OF_YEAR) == calCur.get(Calendar.DAY_OF_YEAR)){
events.add(new Event(start,end,name,description));
}else{
days.add(new Day(323,events)); // HERE I CREATE NEW DAY AND ADD IT TO ARRAY
events.clear();
events.add(new Event(start,end,name,description));
}
}
}
}