在子文档中获取数据

时间:2019-12-04 14:31:50

标签: java android firebase google-cloud-firestore

enter image description here enter image description here

大家好,您能帮我解决我的问题吗?我需要在子文档的TextView中显示所有数据(“时间”)。帮帮我,我是菜鸟。

db3.collection("Blood_Glucose").document(uid).collection("Date").document(date).collection("Time").document(time).set(answer);

1 个答案:

答案 0 :(得分:1)

要获取该文档中的数据,您必须进行get()调用并附加一个侦听器,如以下代码行所示:

DocumentReference timeRef = db3.collection("Blood_Glucose").document(uid)
    .collection("Date").document(date)
    .collection("Time").document(time);

假设:

uid = rom.reyes2398@yahoo.com
date = December 04, 2019
time = 03:15 AM

例如,要获取Result的值,请使用以下代码行:

timeRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String result = document.getString("Result");
                String date = document.getString("Date");
                String time = document.getString("Time");
                Log.d(TAG, result + " / " + date  + " / " + time);
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

您的logcat中的结果将是:

Risk of Having HyperGlycemia / December 04, 2019 / 03:15 AM