簡體   English   中英

嘗試從Firestore獲取字符串

[英]Trying to get a String from the firestore

我正在開發受限的注冊頁面,我需要從數據庫中獲取代碼,但是字符串返回為空,我不知道為什么。 有人可以幫忙嗎?

這是讀取代碼的功能:

public void codeClinic() {

    DocumentReference docRef = FirebaseFirestore.getInstance().collection("CodeClinic").document("Codes");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                codedb = document.getString(clinic.getText().toString());
                Log.d("Code",codedb);
            }
        }
    });
}

我只是在onClick函數上的TextView上調用它,以查看返回的內容,然后返回空的。

這是數據庫

數據庫

謝謝

要使其正常工作,請使用以下代碼:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("CodeClinic").document("Codes").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String clinica = document.getString("Clinica");
                Long feup = document.getLong("FEUP");
                String outra = document.getString("outra");
                Log.d("TAG", clinica + " / " + feup + " / " + outra);
            }
        }
    }
});

您的輸出將是: 1111 / 1234 / 2222

暫無
暫無

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

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