首页 > 解决方案 > 使用 Firebase 实时数据库检查 Recyclerview 是否为空

问题描述

我已经使用firebase实时数据库在片段中创建了一个Recyclerview,我想检查recyclerview是否为空我如何在addValueEventlistner的有趣数据快照中访问bookadapter.itemcount,并且还在适配器中添加了一些条件以删除Recyclerview中的某些项目但无法检查recyclerview是否为空,请解决我的问题。

书籍.kt

 private fun showbooks() {
    databaseReference.addValueEventListener(object : ValueEventListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onDataChange(snapshot: DataSnapshot) {
            try {
                booklist.clear()
                for (item in snapshot.children) {
                    val bookid = item.child("BookId").value.toString()
                    val bookname = item.child("BookName").value.toString()
                    val bookpublication = item.child("BookPublication").value.toString()
                    val bookdept = item.child("Department").value.toString()
                    val bookimage = item.child("BookImage").value.toString()
                    val bookyear = item.child("BookYear").value.toString()
                    val userid = item.child("UserUID").value.toString()
                    val date = item.child("Date").value.toString()
                    RedeleteBook(date, bookimage, bookid)
                    booklist.add(
                        Booksmodel(
                            bookid,
                            bookimage,
                            bookname,
                            bookdept,
                            bookpublication,
                            bookyear,
                            userid,
                        )
                    )
                    bookadapter =
                        bookadapter(
                            booklist,
                            this@Books
                        )
                    bookadapter.notifyDataSetChanged()
                    bookrecyclerView.adapter =
                        bookadapter
                    bookprogressbar.visibility =
                        View.GONE
                    
                }

            } catch (e: Exception) {
                Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
                bookprogressbar.visibility = View.GONE
            }
        }

        override fun onCancelled(error: DatabaseError) {
            Toast.makeText(context, "please check connection", Toast.LENGTH_SHORT).show()
            bookprogressbar.visibility = View.GONE
        }
    })

书适配器.kt

package com.bookqueen.bookqueen.adapter

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 {
    Log.d("Booksize",(booklist.size.toString()))
    return if (booklist.size == 0) 0 else 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)
    val bookview = view.findViewById<CardView>(R.id.bookcardView)

 

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

        val database = FirebaseDatabase.getInstance()
        val auth = FirebaseAuth.getInstance()
        Mycollege.college(object : Mycollege.Mycallback {
            override fun onCallback(value: String) {
                Log.d("Mycoolege", value)
                database.getReference("Users").child(book.UserUID.toString())
                    .addListenerForSingleValueEvent(object : ValueEventListener {
                        @SuppressLint("NotifyDataSetChanged")
                        override fun onDataChange(snapshot: DataSnapshot) {
                            if (snapshot.value != null) {
                                val usercollege = snapshot.child("College").value.toString()

                                if (usercollege == value) {

                                    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)
                                        .error(R.drawable.ic_action_book)
                                        .into(bookimage)
                                } else {
                                    booklist.remove(book)
                                    notifyDataSetChanged()
                                   
                                }

                            }
                        }

                        override fun onCancelled(error: DatabaseError) {

                        }
                    })
            }
        })


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

}

interface OnBookItemClicklistner {
    fun onBookItemclick(books: Booksmodel)
}

}

标签: androidfirebasefirebase-realtime-databaseandroid-recyclerview

解决方案


推荐阅读