簡體   English   中英

Android:從子集合中獲取字段值

[英]Android: Get field values from sub-collection

有沒有辦法從子集合中獲取數據? 下面是我的數據庫的結構,對於每個用戶,只會在“userinfo”子集合中創建一個文檔來存儲用戶名和 email。

我正在嘗試從 Firestore 獲取“用戶名”和“電子郵件”值,然后將這些值設置為字符串,例如:

字符串用戶名=? 字符串 email =?

"main"(root_collection)
            |
            |=>userid1(auto-generated)
                         |                  
                         |=>"userinfo"(sub-collection)=>documentID(auto)=>username: mary
                         |                                              =>email:mary@gmail.com
                         |                                             
                         |                            
                         |=>"emotions"(sub-collection)=>documentID(auto)=>happy: true
                                                                        =>sad:false
                                                                        =>depressed:false

我從其他問題中閱讀了一些其他答案,說可以使用

String value = documentSnapshot.getString("field");

但我仍然不知道該怎么做? 我寫了一些東西,我不知道如何獲得價值。

 FirebaseAuth mAuth = FirebaseAuth.getInstance();
        String userId = Objects.requireNonNull(mAuth.getCurrentUser()).getUid();

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference userRef = db.collection("main").document(userId).collection("userinfo");

        userRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                //get the value
                String value = queryDocumentSnapshots.getDocuments("username"); // how ??
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

還有一個 object class UserInfo

import java.io.Serializable;

public class UserInfo implements Serializable {

    private String email, username;

    public UserInfo() {}

    public UserInfo(String email, String username) {
        this.email = email;
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

請幫忙,非常感謝。

有沒有辦法從子集合中獲取數據?

就在這里。 要獲取該數據,您必須按照以下代碼行遍歷結果:

userRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                String value = document.getString("username");
                Log.d("TAG", value);
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

logcat 中的結果將是:

mary

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM