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

kar*_*tik 1 java android firebase google-cloud-firestore

我正在使用此代码:

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());
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

但我不知道如何获得。我知道put()在 Cloud Firestore 上插入数据的帮助下,但我不知道如何获取放置在user_01.

Ale*_*amo 5

假设您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());
        }
    }
});
Run Code Online (Sandbox Code Playgroud)