java - 有没有办法从android中的firestore中的数组获取数据

标签 java android firebase google-cloud-firestore

我想从user_stories获取数据,例如story和story_startstory end

这是我的 Firestore 数据库的图像。

enter image description here

最佳答案

正如我在您的数据库架构中看到的,您的 user_stories 属性是数组类型。这意味着,当您尝试获取该文档时,您会以对象的List 形式获取该属性,而不是以数组的形式获取该属性。不幸的是,Firestore 无法将该列表转换为自定义对象列表,您必须自己执行此操作。所以请尝试这个:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference sendStoriesRef = rootRef.collection("sendstories");
sendStoriesRef.document("kueIJcdQARzEKU7EVio9").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) document.get("user_stories");

                //Iterate through the list to get the desired values
            }
        }
    }
});

现在只需迭代列表即可获取所需的值。如果 kueIJcdQARzEKU7EVio9 是用户的 uid,则只需使用以下 uid 更改该值即可:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

关于java - 有没有办法从android中的firestore中的数组获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765444/

相关文章:

iOS:在 pod 中使用 Segment-Firebase 时构建 Xcode 项目时出错

java - Android下的测试境界

java - Java中的数组列表

java - 通过带有 excel 文件的 java 应用程序发送电子邮件 - 无法正常工作

Android:为什么我无法为 TextView 背景使用选择器

android - 发现flutter中任务 ':app:checkDebugManifest'(类型 'CheckManifest')的配置有问题

javascript - 在 React 应用程序中查看/管理 Firebase 监听器的数量

java - 在 Android 中使用 firestore 仅使用用户名和密码进行身份验证

java - 类中几个方法的强制签名

android - 如何在Android中播放Base64编码的音频?