如何按最新消息对聊天列表进行排序? [英] How can I sort chat list by the most recent message?

查看:159
本文介绍了如何按最新消息对聊天列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我陷入一个问题,因为 chatList 不能按上一条消息时间或最新消息进行排序.我尝试通过在数据库中存储timestamp orderChildBy 时间戳进行尝试,但是仍然无法正常工作.

I don't know why I got stuck on a problem that the chatList is not sorting by the last message time or by the most recent message. I have tried by storing timestamp in the database and orderChildBy timestamp but it still not working.

这是我在firebaseDatabase

    val timeAgo = Date().time

    val myTimeMap = HashMap<String, Any?>()
        myTimeMap["timestamp"] = timeAgo.toString()
        myTimeMap["id"] = friendId

    val friendTimeMap = HashMap<String, Any?>()
        friendTimeMap["timestamp"] = timeAgo.toString()
        friendTimeMap["id"] = currentUserID

    val chatListSenderReference = dbRef.child("ChatList").child(currentUserID).child(friendId)
        chatListSenderReference.keepSynced(true)
        chatListSenderReference.addListenerForSingleValueEvent(object : ValueEventListener{
              override fun onCancelled(p0: DatabaseError) {
              }
              override fun onDataChange(p0: DataSnapshot) {
                       if(!p0.exists()){
                             chatListSenderReference.updateChildren(friendTimeMap)
                       }
    val chatListReceiverReference = dbRef.child("ChatList").child(friendId).child(currentUserID)
        chatListReceiverReference.updateChildren(myTimeMap)
        }
    })

在recyclerView中检索聊天列表

on retrieving the chatlist in recyclerView

 mUsers = ArrayList()
        val userRef = dbRef.child("ChatList").child(currentUserID).orderByChild("timestamp")
        userRef.addValueEventListener(object : ValueEventListener
        {
            override fun onCancelled(error: DatabaseError) {
            }

            override fun onDataChange(snapshot: DataSnapshot)
            {
                (mUsers as ArrayList).clear()
                snapshot.children.forEach {
                    val userUid = it.key
                    if (userUid != null) {
                        (mUsers as ArrayList).add(User(uid = userUid))
                    }
                }
                retrieveGroupChatList()
                chatListAdapter?.notifyDataSetChanged()
                chatListAdapter = context?.let { ChatListAdapter(it, (mUsers as ArrayList<User>), true) }
                recyclerViewChatList.adapter = chatListAdapter
            }
        })

这是数据库的图片,每次我发送或接收消息时间戳获取更新时.

this is the picture of database, every time when i send or receive message timestamp get update.

推荐答案

您应该存储消息的时间戳,然后使用val chatListSenderReference = dbRef.child("ChatList").child(currentUserID).orderByChild("timestamp")

You should store the timestamp of messages and then use val chatListSenderReference = dbRef.child("ChatList").child(currentUserID).orderByChild("timestamp")

这篇关于如何按最新消息对聊天列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆