0

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

This is the way I created chatList in the firebaseDatabase:

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

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 the database, every time when I send or receive a message timestamp gets an update.

enter image description here

mr. groot
  • 334
  • 4
  • 18
Ggonstack
  • 31
  • 5

3 Answers3

0

You need to specify another child after current user ID, because you have in each user id a list of IDs.

Tawfik Yasser
  • 86
  • 2
  • 5
  • I tried your suggestion like dbRef.child("ChatList").child(currentUserID).child("chat").orderByChild("timestamp") at the time of storing and at the time of retrieving. But it didn't work ... Is this that you were suggesting? – Ggonstack Nov 22 '20 at 06:25
  • Hey there i posted a new question here with full details please answer https://stackoverflow.com/q/64968904/14647144 – Ggonstack Nov 23 '20 at 13:10
0

If you want to get accurate results, then you would have to change the type of your timestamp field from String to a number. You need this change is because the order of String values is lexicographically. So you should change this in the database, as well as in your class.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • "still not working" doesn't help! What exactly doesn't work? Do you have any errors? – Alex Mamo Nov 22 '20 at 14:46
  • Whenever i message the timestamp of the chatlist between currentUserId and friendID's has updated so they should be on the top in the recyclerview but it doesn't. I turned the value of timestamp from string to long but it still not doing the desired working. – Ggonstack Nov 22 '20 at 15:08
  • In that case, I recommend you post a new question using its own [MCVE](https://stackoverflow.com/help/mcve) with what you have tried, so I and other Firebase developers can help you. – Alex Mamo Nov 22 '20 at 15:28
  • Sir Alex as you told me to ask a new MCVE question i have asked with another id here please answer this and solve my problem then i will accept your answer and upvote that from both my IDs. https://stackoverflow.com/q/64968904/14647144 – Ggonstack Nov 23 '20 at 13:08
  • @Ggonstack I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Nov 23 '20 at 17:29
  • @Ggonstack However, that's a totally different problem. I was asking if I can help you with other information regarding the timestamp property? If not, please consider accepting my answer. I'd really appreciate it. Thanks! – Alex Mamo Nov 23 '20 at 17:31
  • Alex what you answered that to change string to number (long) i already had tried that before your answer so it is not helpful for me. – Ggonstack Nov 24 '20 at 07:08
0

Do the below changes:

  1. Get order by key
  2. If you later want the time of the message(i.e. to display it in the chat message) then before pushing the changes to Firebase, store the time in your chat message Model.
  3. Update your json structure by updating your chat message model to below,
 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)
HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • I think you are talking about the chat model class which contains the information of message , that is something else... I will post another question on this with MCVE, and i will mention the link here so please answer there. – Ggonstack Nov 23 '20 at 07:15
  • Hey there i posted the new question with more details here please answer https://stackoverflow.com/q/64968904/14647144 – Ggonstack Nov 23 '20 at 13:09