0

Model Class (Data Class)

data class Message(var userID:String? = "",var message:String? = "",var recID:String? = "",var isSeen:String="")

I have an an adapter that is supposed to change the value in a particular TextView based on the value of the isSeen field in my document. However this change does not happen, I thought that the adapter would make the change automatically when i call the notifyDataSetChanged() but since it did not, I added some event listener, listener registration and yet the TextView doesn't change when the isSeen changes from false to true. It only displays what it should display when the field is false.

onBindViewHolder inside Adapter Class

override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
    val message:Message = MessageList[position]
        if(position==MessageList.size-1){
            if(message.isSeen=="true")
            {
                holder.textSeen.text = "Seen"
            }else if(message.isSeen=="false"){
                holder.textSeen.text = "Delivered"
            }
        }else{
            holder.textSeen.visibility = View.GONE
        }
    }

Activity Class (where the value inside database successfully changes from false to true)

eventListener = EventListener { snapshot, _ ->
            if (snapshot != null) {
                val message: Message? = snapshot.toObject(Message::class.java)
                if (message != null) {
                    if (message.recID.equals(userID) && message.userID.equals(recID)) {
                        val hashMap: HashMap<String, Any> = HashMap()
                        hashMap["isSeen"] = "true"
                        message.isSeen = "true"
                        snapshot.reference.update(hashMap)
                        messageAdapter.notifyDataSetChanged()
                    }
                }
                messageAdapter.notifyDataSetChanged()
            }
        }
private fun checkSeen() {
        FirebaseFirestore.getInstance().collection("chats").document(roomID).collection("messages")
            .addSnapshotListener(EventListener { results, e ->
                if (e != null) {
                    Log.w(TAG, "Listen failed.", e)
                    return@EventListener
                }
                for (doc in results!!) {
                    val message: Message = doc.toObject(Message::class.java)
                    if (message.recID.equals(userID) && message.userID.equals(recID)) {
                        val hashMap: HashMap<String, Any> = HashMap()
                        hashMap["isSeen"] = "true"
                        message.isSeen = "true"
                        doc.reference.update(hashMap)
                        messageAdapter.notifyDataSetChanged()
                    }
                }
            })
        messageAdapter.notifyDataSetChanged()
    }

inside database when Sender sends the message TextView shows delivered

After Receiver opens the message inside database when Receiver opens the message

But for sender it still shows Delivered and not Seen.

  • What's wrong with the shared code? – Alex Mamo Mar 01 '22 at 11:44
  • Hi @AlexMamo, I did some trouble shooting on my own and found out that for some reason the isSeen field was returning a blank/empty string but when I changed it to some different value like iss for example it worked correctly as i wanted it to and i was getting "true" and "false" properly. Do you have any idea why this happened? – Aaron DCosta Mar 01 '22 at 12:02
  • Are you using Hashmap as a list for your adapter? Beacuse I cant see the updation on the list sent to adapter. There was no updation on the Messagelist but you are setting values from Message object, I think if you update the MessageList's last element's isSeen to "true" might work – Mathi Vanan Mar 01 '22 at 13:36

1 Answers1

0

try this code

 if(position==MessageList.size-1){
            if(message.isSeen=="true")
            {
                holder.textSeen.text = "Seen"
            }else if(message.isSeen=="false"){
                holder.textSeen.text = "Delivered"
            }
            holder.textSeen.visibility = View.VISIBLE <-- add this line
        }else{
            holder.textSeen.visibility = View.GONE
        }
GHH
  • 1,713
  • 1
  • 8
  • 21