【问题标题】:problem with the getting data from firestore into recyclerView从firestore获取数据到recyclerView的问题
【发布时间】:2022-01-07 14:23:47
【问题描述】:

我正在尝试在我的应用中实现消息页面。我从 Firestore 接收消息。当用户单击 RecyclerView 项时,isMessageRead 值更改为 true。一切正常,但问题是当使用模型类从 Firestore 读取布尔值时,所有值都变为 false。我错过了什么?谢谢。

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

        }
    }
}

【问题讨论】:

    标签: android firebase kotlin google-cloud-platform google-cloud-firestore


    【解决方案1】:

    当您尝试将文档映射到NotificationModelClass 类型的对象时,这意味着类中的每个字段都将使用文档中存在的相应字段的值进行初始化。由于isMessageRead 字段已经使用false 的值进行了初始化,因此在反序列化时,该字段的值将始终为false。要解决这个问题,你必须改变:

    var isMessageRead: Boolean? = false,
    

    收件人:

    val isMessageRead: Boolean? = null,
    

    这样,您将使用来自数据库的值而不是默认值来初始化字段。

    【讨论】:

    • 感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2021-02-04
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多