我不知道为什么我陷入一个问题,因为 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")
更多推荐
如何按最新消息对聊天列表进行排序?
发布评论