简体   繁体   English

如何在Android中从Cloud Firestore Firebase获取或获取数据

[英]How to fetch or get data from Cloud Firestore Firebase in android

I am using this code: 我正在使用此代码:

DocumentReference docRef = db.collection("Users").document("user_01");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null && document.exists()) {

            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

But I don't know how to get. 但是我不知道该怎么去。 I know with the help of put() I able to insert data on Cloud Firestore, but I don't know how to fetch data which is placed in user_01 . 我知道可以在put()的帮助下在Cloud Firestore上插入数据,但是我不知道如何获取放置在user_01

Assuming you have a property caled name under each user object, to solve this, please use the following code: 假设每个用户对象下都有一个属性name ,要解决此问题,请使用以下代码:

DocumentReference docRef = db.collection("Users").document("user_01");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null && document.exists()) {
                Log.d("TAG", document.getString("name")); //Print the name
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM