java - 从 Cloud Firestore 获取数据

标签 java android firebase google-cloud-firestore

Picture

我需要从 Firestore Firebase 获取数据,我需要从“雇主”集合 -> “TH17...”文档中获取字段“公司”的值。我尝试了一些方法,但没有用。

我该怎么做?

我试试这个:

docref = db.collection("employer").document("TH17...");
        docref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        String p= document.getData().toString();
                        Log.d("TAG", "DocumentSnapshot data: " + document.getData());
                    } 
                }
            }
        });

最佳答案

I need to get the value of field "company" from "employer" collection -> "TH17..." document.

要解决这个问题,请更改以下代码行:

String p= document.getData().toString();
Log.d("TAG", "DocumentSnapshot data: " + document.getData());

String company = document.getString("Company");
Log.d("TAG", "Company: " + company);

您的 logcat 中的结果将是:

Company: Test

关于java - 从 Cloud Firestore 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53230921/

相关文章:

java - Android:公历问题

java - 查找两个 2 TSV 文件之间的唯一元素

java - 不眨眼地更新 Flex 中的照片

c# - WCF - 使用 NetHttpBinding(WebSockets) 或替代双工绑定(bind)发送 JSON

android - 服务器更新设备未使用

Java Swing - 制作两个 JLists "siblings"- 即只能选择其中一个项目

安卓工作室 : how add tabs with ViewPager

javascript - Firebase 实时数据库读取操作 - 只能获取返回的部分文档?

android - Firebase 谷歌身份验证不工作 android

firebase - 如何将 Cloud Firestore 数据库集合下载到 JSON 或 CSV 文件中?