【问题标题】:Chat-list is not getting sorted by the most recent message with firebase聊天列表未按使用 firebase 的最新消息进行排序
【发布时间】:2020-11-22 05:45:32
【问题描述】:

我不知道为什么我遇到了 chatList 没有按最后消息时间或最近消息排序的问题。我尝试将timestamp 存储在数据库中并使用 orderChildBy 时间戳,但它仍然无法正常工作。

这是我在firebaseDatabase中创建chatList的方式:

    val timeAgo = Date().time

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

    val friendTimeMap = HashMap<String, Any?>()
        friendTimeMap["timestamp"] = timeAgo
        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 中检索聊天列表

 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
            }
        })

这是数据库的图片,每次发送或接收消息时时间戳都会更新。

【问题讨论】:

    标签: java android firebase sorting firebase-realtime-database


    【解决方案1】:

    您需要在当前用户 ID 之后指定另一个孩​​子,因为您在每个用户 ID 中都有一个 ID 列表。

    【讨论】:

    【解决方案2】:

    如果您想获得准确的结果,则必须将 timestamp 字段的类型从字符串更改为数字。您需要此更改是因为字符串值的顺序是lexicographically。因此,您应该在数据库以及您的班级中更改它。

    【讨论】:

    • “仍然不工作”没有帮助!究竟是什么行不通?你有什么错误吗?
    • 每当我发送消息时,currentUserId 和friendID 之间的聊天列表的时间戳已经更新,因此它们应该在recyclerview 的顶部,但它没有。我将时间戳的值从字符串转换为长,但它仍然没有做预期的工作。
    • 在这种情况下,我建议您使用自己的MCVE 发布一个新问题,其中包含您尝试过的内容,以便我和其他 Firebase 开发人员可以帮助您。
    • 亚历克斯爵士,您告诉我要问一个新的 MCVE 问题,我在这里用另一个 id 提出过,请回答这个问题并解决我的问题,然后我会接受您的回答并从我的两个 ID 中投票。 stackoverflow.com/q/64968904/14647144
    • @Ggonstack 我去看看,如果我知道答案,我会写信给你。
    【解决方案3】:

    进行以下更改:

    1. 按键获取订单
    2. 如果您稍后想要消息的时间(即显示在聊天消息中),那么在将更改推送到 Firebase 之前,请将时间存储在您的聊天消息模型中。
    3. 通过将您的聊天消息模型更新到下面来更新您的 json 结构,
     ChatMessage(Your message model class)
      -uid  (id of the user that sent the message)
      -name (name of the person that sent the chat message)
      -message
      -timestamp (time when user sent the message)
    

    【讨论】:

    • 我想你说的是包含 message 信息的聊天模型类,那是另外一回事......我将在 MCVE 上发布另一个问题,我会在这里提到链接请在那里回答。
    • 嘿,我在这里发布了更多详细信息的新问题,请回答stackoverflow.com/q/64968904/14647144
    猜你喜欢
    • 2021-03-06
    • 2021-02-27
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多