0

I have made an recyclerview with firebase realtime database and applied some condition in adapter class,applied condition works but recyclerview item didn't show data in card view it show only cardview but in searchview it shows data correctly in cardview; I have added screenshot please solve my problem.

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

Zain
  • 37,492
  • 7
  • 60
  • 84
Ashish Dawkhar
  • 131
  • 1
  • 9

1 Answers1

0

add notifydatasetchanged after booklist.remove(book)

Ashish Dawkhar
  • 131
  • 1
  • 9
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '21 at 12:48