【发布时间】:2018-08-19 09:57:28
【问题描述】:
我正在使用 Cloud Firestore 来填充 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():
public override fun onStart() {
super.onStart()
val user = FirebaseAuth.getInstance().currentUser
val isUserFirstTime = java.lang.Boolean.valueOf(Utility.readSharedSetting(applicationContext, Utility.PREF_USER_FIRST_TIME, "true"))
val introIntent = Intent(this@MainActivity, OnBoardActivity::class.java)
introIntent.putExtra(Utility.PREF_USER_FIRST_TIME, isUserFirstTime)
if (isUserFirstTime) {
startActivity(introIntent)
finish()
} else if (user == null) {
startActivity(Intent(this@MainActivity, WelcomeActivity::class.java))
finish()
} else if (!isUserFirstTime) {
val firstQuery = shopsCollection.orderBy("timestamp", Query.Direction.DESCENDING).limit(3)
firstQuery.addSnapshotListener({ documentSnapshots, e ->
lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
for (doc in documentSnapshots) {
showFabButton()
val blogPost = doc.toObject(Shop::class.java)
shopList.add(blogPost)
mAdapter.notifyDataSetChanged()
}
})
}
}
}
【问题讨论】:
-
有更新时是你的
addSnapshotListener吗?例如。如果您在那里添加一些日志记录或设置断点,它是否会在有更新时记录/触发? -
但它不起作用,更改仅显示重新启动应用程序
-
@user8209569 这正是弗兰克想要弄清楚的。可能是没有调用 Listener,列表没有正确更改,或者更新后更改可能无法从列表正确反映到视图。
-
请在
RecyclerView中发布您的适配器以及您设置适配器的方式。 -
P.S.您在添加每个项目后调用 notifyDatasetChanged 。将其移出 for 循环
标签: android firebase-realtime-database android-recyclerview kotlin google-cloud-firestore