如何按最新消息对聊天列表进行排序?

编程入门 行业动态 更新时间:2024-10-22 18:41:18
本文介绍了如何按最新消息对聊天列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道为什么我陷入一个问题,因为 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")

更多推荐

如何按最新消息对聊天列表进行排序?

本文发布于:2023-10-24 14:37:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最新消息   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!