0

我的班级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));
                }
            }
        }
    }
4

1 回答 1

0

您可以使用此方法获取唯一日期,然后将每天的事件添加到哈希图中private HashMap<Integer, ArrayList<YourModel>> eventInfo = new HashMap<Integer, ArrayList<YourModel>>();

private List<String> getUniqueDays(List<YourModel> list)
{
    Set<String> res = new LinkedHashSet<String>();

    for (int i = 0; i < list.size(); i++)
    {
        if (list.get(i).getDay().length() > 0)
            res.add(list.get(i).getDay());
    }

    List<String> temp = new ArrayList<String>(res);
    Collections.sort(temp, dateComparator);
    return temp;
}

Comparator<String> dateComparator = new Comparator<String>()
{
    @Override
    public int compare(String lhs, String rhs)
    {
        try
        {
            DateFormat format = new SimpleDateFormat("MM-dd-yyyy h:mm:ss a");
            return format.parse(lhs).compareTo(format.parse(rhs));
        }
        catch (Exception e)
        {
            return 0;
        }
    }
};
于 2012-11-24T22:40:07.527 回答