java - 如何将 .addSnapShotListener 设置为 fragment 中的集合引用

标签 java android firebase google-cloud-firestore

在我的应用程序中,我想在从 Firestore 获取数据之前添加加载布局。

所以我尝试添加 Snapsnot 监听器,但效果不佳。

我想知道我该怎么做。

最佳答案

为了能够使用 addSnapshotListener,请尝试以下操作:

final DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        if (snapshot != null && snapshot.exists()) {
            Log.d(TAG, "Current data: " + snapshot.getData());
        } else {
            Log.d(TAG, "Current data: null");
        }
    }
});

document() 返回 DocumentReference ,并且类 DocumentReference 包含方法 addSnapshotListener

关于java - 如何将 .addSnapShotListener 设置为 fragment 中的集合引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60630541/

相关文章:

java - 如何在java中安排一个 Action ?

java - 选项出现在 JMenu 上

android - 搜索功能不工作 - Android

android - 从 2 个应用程序访问相同的 SQLite 数据库

javascript - 我正在尝试查询 firestore 中的时间戳

swift - 单击mapview注释时如何将firebase数据信息传递到另一个 View Controller ?

java - 如何允许我的 Maven 插件访问使用它的项目的资源?

java - 如何修复字符串索引超出范围 : -1

android - 如何在 android 中将 Bitmap 转换为 Drawable?

android - 使用 play-services-ads 添加 firebase 时出现重复类构建错误