Android Firestore实时更新在recyclerview上不起作用

Android Firestore实时更新在recyclerview上不起作用,android,firebase-realtime-database,android-recyclerview,kotlin,google-cloud-firestore,Android,Firebase Realtime Database,Android Recyclerview,Kotlin,Google Cloud Firestore,我正在使用CloudFireStore填充recyclerview 但是,实时更新不起作用,重新启动应用程序后会显示更改。我还在recyclerview上添加了分页。文档添加和修改仅在重新启动应用程序时显示 要加载我的帖子: private fun loadMorePost() { val nextQuery = shopsCollection .orderBy("timestamp", Query.Direction.DESCENDING)

我正在使用CloudFireStore填充recyclerview

但是,实时更新不起作用,重新启动应用程序后会显示更改。我还在recyclerview上添加了分页。文档添加和修改仅在重新启动应用程序时显示

要加载我的帖子:

private fun loadMorePost() {
    val nextQuery = shopsCollection
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .startAfter(lastVisible)
            .limit(3)

    nextQuery.addSnapshotListener({ documentSnapshots, e ->
        if (!documentSnapshots.isEmpty) {

            lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
            for (doc in documentSnapshots) {

                    val shop = doc.toObject(Shop::class.java)
                    shopList.add(shop)

                    mAdapter.notifyDataSetChanged()

                }
            }
    })
}
onStart():


我假设您引用的数据库的更改是删除:

在代码中添加新项,但不删除可能的删除项。因此,将代码更改为以下内容应该可以:

nextQuery.addSnapshotListener({ documentSnapshots, e ->
    if (!documentSnapshots.isEmpty) {
        shopList.clear()
        lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
        for (doc in documentSnapshots) {
                val shop = doc.toObject(Shop::class.java)
                shopList.add(shop)
                mAdapter.notifyDataSetChanged()
            }
        }
})

有更新时,您的
是否添加SnapshotListener
?例如,如果您在其中添加了一些日志记录或设置了一个断点,它是否会在有更新时记录/触发?但它不起作用,只会在重新启动时显示更改app@user8209569这就是弗兰克想弄明白的。可能是未调用侦听器、列表未正确更改或更新后列表中的更改未正确反映到视图中。请在
RecyclerView
中发布适配器以及设置适配器的方式。每次添加项目后,您都会调用notifyDatasetChanged。将其移出for循环
nextQuery.addSnapshotListener({ documentSnapshots, e ->
    if (!documentSnapshots.isEmpty) {
        shopList.clear()
        lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
        for (doc in documentSnapshots) {
                val shop = doc.toObject(Shop::class.java)
                shopList.add(shop)
                mAdapter.notifyDataSetChanged()
            }
        }
})