繁体   English   中英

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

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

我正在使用此代码:

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());
        }
    }
});

但是我不知道该怎么去。 我知道可以在put()的帮助下在Cloud Firestore上插入数据,但是我不知道如何获取放置在user_01

假设每个用户对象下都有一个属性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