首页 > 解决方案 > Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据

问题描述

我制作了一个recyclerview带有firebase实时数据库并在适配器类中应用了一些条件,应用条件有效但recyclerview项目没有在卡片视图中显示数据,它只显示cardview但在searchview中它正确显示数据cardview; 我添加了屏幕截图,请解决我的问题。

package com.bookqueen.bookqueen.adapters

class bookadapter(
private var booklist: ArrayList<Booksmodel>,
private val itemClickListener: OnBookItemClicklistner
) : RecyclerView.Adapter<bookadapter.bookholder>() {

var searchText: String = ""


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): bookholder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.singlebook, parent, false)
    return bookholder(view)
}

@SuppressLint("NotifyDataSetChanged")
fun filterlist(filterlist: ArrayList<Booksmodel>, searchText: String) {
    booklist = filterlist
    this.searchText = searchText
    notifyDataSetChanged()

}

override fun onBindViewHolder(holder: bookholder, position: Int) {
    val view = booklist[position]

    holder.bind(view, itemClickListener)


}

override fun getItemCount(): Int {
    return booklist.size
}

inner class bookholder(view: View) : RecyclerView.ViewHolder(view) {
    val bookname: TextView = view.findViewById(R.id.recbooknametxt)
    val bookpublication = view.findViewById<TextView>(R.id.recbookpubtxt)
    val bookdept = view.findViewById<TextView>(R.id.recbookdepttxt)
    val bookimage = view.findViewById<ImageView>(R.id.recbookimg)

   
    fun bind(book: Booksmodel, clicklistner: OnBookItemClicklistner) {

        val database = FirebaseDatabase.getInstance()
        val auth = FirebaseAuth.getInstance()
        database.getReference("Users").child(book.UserUID.toString())
            .addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    if (snapshot.value != null) {
                        val usercollege = snapshot.child("College").value.toString()
                        database.getReference("Users")
                            .child(auth.currentUser!!.uid)
                            .addListenerForSingleValueEvent(object :
                                ValueEventListener {
                                override fun onDataChange(snapshot: DataSnapshot) {
                                    if (snapshot.value != null) {
                                        val mycollege =
                                            snapshot.child("College").value.toString()
                                        if (usercollege == mycollege) {
                                            searchitem(book)
                                        } else {
                                            booklist.remove(book)
                                        }
                                    }
                                }

                                override fun onCancelled(error: DatabaseError) {
                                }
                            })
                    }
                }

                override fun onCancelled(error: DatabaseError) {

                }
            })

        itemView.setOnClickListener {
            clicklistner.onBookItemclick(book)
        }
    }


    fun searchitem(book: Booksmodel) {
        if (searchText.isNotBlank()) {
            val highlightedText = book.BookName!!.replace(
                searchText,
                "<font color='red'>$searchText</font>",
                true
            )
            bookname.text =
                HtmlCompat.fromHtml(
                    highlightedText,
                    HtmlCompat.FROM_HTML_MODE_LEGACY
                )
        } else {
            bookname.text = book.BookName
        }
        //bookname.text=book.BookName
        bookpublication.text = book.BookPublication
        bookdept.text = book.Department
        Picasso.get().load(book.BookImage).into(bookimage)
    }
}

interface OnBookItemClicklistner {
    fun onBookItemclick(books: Booksmodel)
}

}

截图链接

标签: androidkotlinandroid-recyclerviewadapterrealmrecyclerviewadapter

解决方案


在 booklist.remove(book) 之后添加 notifydatasetchanged


推荐阅读