本文介绍了检查是否有“无文件"状态或FirebaseUI的FirebaseRecyclerAdapter中的数据集为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基于FirebaseUI 文档<这样:
I wrote a custom FirebaseRecylerAdapter, based on FirebaseUI documentation like this:
class FavoritesAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Favorite, FavoritesAdapter.FavoritesHolder>(buildOptions(lifecycleOwner)) {
companion object {
private fun buildQuery() = FirebaseDatabase.getInstance()
.reference
.child("favorites")
.limitToLast(50)
private fun buildOptions(lifecycleOwner: LifecycleOwner) = FirebaseRecyclerOptions.Builder<Favorite>()
.setQuery(buildQuery(), Favorite::class.java)
.setLifecycleOwner(lifecycleOwner)
.build()
}
//...
此类重写onDataChanged()函数:
This class overrides an onDataChanged() function:
override fun onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
现在,我该如何实际检查该无文档"状态以更新我的UI?我找不到方法检查空数据集.我正在寻找类似getItemCount()的东西.
Now, how do I actually check for that "no documents" state in order to update my UI? I can find no way to check for an empty data-set. I am looking for something like getItemCount().
推荐答案
适配器确实具有itemCount
属性,您还可以使用snapshots
获取模型对象的实时列表.示例:
The adapter does have an itemCount
property and you can also use snapshots
to get a live list of your model objects. Example:
override fun onDataChanged() {
if (itemCount == 0) {
// Do stuff
}
}
这篇关于检查是否有“无文件"状态或FirebaseUI的FirebaseRecyclerAdapter中的数据集为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!