android - 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)
}
}
解决方案
在 booklist.remove(book) 之后添加 notifydatasetchanged
推荐阅读
- php - 如何显示来自不同表的类别名称而不是类别 ID
- python - 对数千个坐标进行反向地理编码以仅获取国家/地区
- java - 单击时正确生成整数
- php - PHP:编辑 JSON 字符串,传递变量值
- jquery - 如果类不可见,则 HTML 显示元素
- javascript - Lodash函数修改字符串数组中的每个元素
- python - 用漂亮的汤从 html 代码中提取表
- reactjs - 状态栏上的 ScrollToTop 单击
- excel - 连接到 PostgreSQL 数据库时在 Power Query 中参数化源
- reactjs - 如何以正确的方式在 ReactJs 应用程序中使用 Tableau WDC