如何按日期对聊天记录进行分组

时间:2014-09-20 04:54:29

标签: android json date chat

我目前正致力于创建Android聊天应用程序。在这里,我有来自Web服务的以下json。我想要的是创建聊天的部分日期,以便聊天在特定日期下分组。

{
    "messages":{
        "rows":[
            {
                "createdDate":"2014-09-13 13:14:07",
                "cID":"36",
                "message":"again with me this is not yet the true chat on android.",
                "mDate":"2014-09-13 11:14:07",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"156",
                "recieverID":"100001031525354"
            },
            {
                "createdDate":"2014-09-15 12:57:44",
                "cID":"36",
                "message":"this is so long long text on the chat application for me",
                "mDate":"2014-09-15 10:57:44",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"158",
                "recieverID":"100001031525354"
            },
            {
                "createdDate":"2014-09-15 13:32:42",
                "cID":"36",
                "message":"is it OK, if I try more and more.",
                "mDate":"2014-09-15 11:32:42",
                "senderID":"100001031525354",
                "readStatus":"2",
                "ID":"159",
                "recieverID":"100000876519562"
            },
            {
                "createdDate":"2014-09-15 18:38:57",
                "cID":"36",
                "message":"what if I typed so long long text over here again and again.",
                "mDate":"2014-09-15 16:38:57",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"166",
                "recieverID":"100001031525354"
            },
            {
                "createdDate":"2014-09-16 11:22:13",
                "cID":"36",
                "message":"hi this is something uncommon",
                "mDate":"2014-09-16 09:22:13",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"167",
                "recieverID":"100001031525354"
            },
            {
                "createdDate":"2014-09-16 13:21:55",
                "cID":"36",
                "message":"it's really",
                "mDate":"2014-09-16 11:21:55",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"170",
                "recieverID":"100001031525354"
            },
            {
                "createdDate":"2014-09-16 17:25:10",
                "cID":"36",
                "message":"yes sure",
                "mDate":"2014-09-16 15:25:10",
                "senderID":"100000876519562",
                "readStatus":"2",
                "ID":"183",
                "recieverID":"100001031525354"
            }
        ]
    },
    "section":{
        "rows":[
            {
                "MessageDate":"2014-09-13",
                "Num":"1"
            },
            {
                "MessageDate":"2014-09-15",
                "Num":"3"
            },
            {
                "MessageDate":"2014-09-16",
                "Num":"3"
            }
        ]
    }
}

这是我尝试过的。嵌套循环的第二个,第三个循环的输出再次从零索引开始。任何想法请帮助。

try {
            JSONObject jObj = new JSONObject(content);
            JSONObject obj = jObj.getJSONObject("messages");
            JSONArray arr = obj.getJSONArray("rows");

            JSONObject objSec = jObj.getJSONObject("section");
            JSONArray arrSec = objSec.getJSONArray("rows");

            List<MsgChat> msgList = new ArrayList<MsgChat>();
            MsgChat ch = new MsgChat();
            for (int i = 0; i < arrSec.length(); i++) {
                JSONObject objHis = arrSec.getJSONObject(i);
                ch.setChatSection(objHis.getString("MessageDate"));
                Log.d("Number of Message", objHis.getString("Num"));

                int chatNum = arrSec.getJSONObject(i).getInt("Num");

                for(int j = 0; j < chatNum; j++) {
                    JSONObject messages = arr.getJSONObject(j);
                    ch.setMsgId(messages.getInt("ID"));
                    ch.setChatMsg(messages.getString("message"));
                    Log.d("Message", messages.getString("message"));
                    ch.setChatDate(messages
                            .getString("createdDate"));
                    ch.setSenderId(Long.parseLong(messages
                            .getString("senderID")));
                    msgList.add(ch);
                }
                msgList.add(ch);
            }

            return msgList;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

1 个答案:

答案 0 :(得分:1)

首先将消息日期与json返回String.try中的createdDate进行比较,此代码为

String a1 = objHis.getString("MessageDate");
String[] a21 = messages.getString("createdDate").split(" ");
String a2  = a21[0].tostring;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(a1);
Date date2 = format.parse(a2);

比较这两个日期,如果它们都相等,请在特定日期聊天记录中添加内容,

if(date1.compareTo(date2)==0)
{
     //add your chat history message for particular date
}