Reputation: 67
I am trying to implement a message page into my app. I take the messages from Firestore. When the user clicks on the RecyclerView item, isMessageRead value is changed to true. Everything is working but the problem is when read the boolean value from Firestore using model class, all of the values are coming false. what did I miss? thanks.
class NotificationAdapter(
var notificationList: List<NotificationModelClass>,
private val clickListener: (NotificationModelClass) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var notifications: List<NotificationModelClass> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val myView = LayoutInflater.from(parent.context)
.inflate(R.layout.notification_layout, parent, false)
return ChaplainsViewHolder(myView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as ChaplainsViewHolder).bind(notifications[position], clickListener)
}
override fun getItemCount(): Int {
return notifications.size
}
fun submitList(notificationList: List<NotificationModelClass>) {
notifications = notificationList
}
class ChaplainsViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val notificationTitle = itemView.title
private val notificationMessage = itemView.message
private val notificationTime = itemView.messageTime
private val notificationImage = itemView.logo
private val notificationCardView = itemView.notificationCardView
private val notificationMessageRead = itemView.imageViewMessageRead
fun bind(
notificationInfo: NotificationModelClass,
clickListener: (NotificationModelClass) -> Unit
) {
notificationTitle.text = notificationInfo.title
notificationMessage.text = notificationInfo.body
val messageTime = notificationInfo.dateSent
val dateFormatLocalZone = SimpleDateFormat("HH:mm - MMM dd, yyyy")
dateFormatLocalZone.timeZone = TimeZone.getDefault()
val timeRangeFirst = dateFormatLocalZone.format(Date(messageTime!!.toLong()))
//notificationTime.text = timeRangeFirst.toString()
Picasso.get().load(R.drawable.logo).into(notificationImage)
notificationTime.text = notificationInfo.isMessageRead.toString()
/*if (isMessageRead == "no"){
notificationMessageRead.setImageResource(R.drawable.ic_baseline_mic_24)
}*/
notificationCardView.setOnClickListener {
clickListener(notificationInfo)
}
}
}
}
Upvotes: 1
Views: 85
Reputation: 138824
When you try to map a document into an object of type NotificationModelClass
it means that each field in the class will be initialized with the value of the corresponding field that exists in the document. Since the isMessageRead
field is already initialized with the value of false
, when deserializing the value of the field will always be false
. To solve this you have to change:
var isMessageRead: Boolean? = false,
To:
val isMessageRead: Boolean? = null,
In this way, you'll initialize the field with a value that comes from the database and not with the default one.
Upvotes: 2