如何从FireStore数据库中获取特定用户的数据并在Android中的RecyclerView中显示

bzzcjhmw  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(93)

我有一个Android应用程序,用户可以在其中上传书籍.在主屏幕上显示所有用户的书.在配置文件部分,有一个按钮名为byBooks,可用于获取当前用户上传的所有书籍.请帮助我获取当前登录用户上传的所有书籍,并在回收器视图中显示它.
这是我的书刀

class BookDao {

    private val db = FirebaseFirestore.getInstance()
    val bookCollection = db.collection("books")

    fun addBook(book: Book?){
        if (book!= null) {
            bookCollection.document(book.bookName).set(book)
        }
    }
}

这就是我如何填充显示所有书籍的家庭片段的回收视图。

val bookDao = BookDao()
        val bookCollection = bookDao.bookCollection
        val query = bookCollection.orderBy("createdAt", Query.Direction.DESCENDING)
        val recyclerViewOptions = FirestoreRecyclerOptions.Builder<Book>().setQuery(query, Book::class.java).build()

        mAdapter = HomeAdapter(recyclerViewOptions)

gojuced7

gojuced71#

我已经得到答案了

val uid = FirebaseAuth.getInstance().uid
        val reference  = FirebaseFirestore.getInstance().collection("books").whereEqualTo("userId", uid)
        val query: Query = reference.orderBy("createdAt")
        val recyclerViewOptions = FirestoreRecyclerOptions.Builder<Book>().setQuery(query, Book::class.java).build()

        mAdapter = HomeAdapter(recyclerViewOptions)

相关问题