Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据

Posted

技术标签:

【中文标题】Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据【英文标题】:Recyclerview items didn't show data in cardview when condition applied to it in adapter 【发布时间】:2021-12-04 18:25:34 【问题描述】:

我用firebase实时数据库创建了一个recyclerview,并在适配器类中应用了一些条件,应用条件有效,但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)



Screenshoot link

【问题讨论】:

【参考方案1】:

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

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据的主要内容,如果未能解决你的问题,请参考以下文章

为什么当我在适配器中调用编辑项目时,我的Recyclerview项目会被克隆?

从 Fragment 中的适配器(recyclerview)获取所有项目

向下滚动时 RecyclerView 项目之间的间隙较大

AdapterView 和 RecyclerView 的连续滚动

可过滤在 Recyclerview 中无法按预期工作

从recyclerview中删除项目(viewholder具有onClick但适配器具有数据集)