【问题标题】:Check for "no documents" state or empty data-set in FirebaseUI's FirebaseRecyclerAdapter检查 FirebaseUI 的 FirebaseRecyclerAdapter 中的“无文档”状态或空数据集
【发布时间】:2018-09-29 12:10:28
【问题描述】:

我基于 FirebaseUI documentation 编写了一个自定义 FirebaseRecylerAdapter,如下所示:

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() 函数:

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() 的东西。

【问题讨论】:

    标签: android firebase firebase-realtime-database kotlin firebaseui


    【解决方案1】:

    适配器确实有一个itemCount 属性,您也可以使用snapshots 来获取模型对象的实时列表。示例:

    override fun onDataChanged() {
        if (itemCount == 0) {
            // Do stuff
        }
    }
    

    【讨论】:

    • 我提到的两种方法都可以在onDataChanged()中使用。我已经用一个例子更新了我的答案。
    • you can also use snapshots to get a live list of your model objects 能否请您在回答中也举个例子?
    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多