簡體   English   中英

Android / FireStore QuerySnapshot轉換為CustomObject

[英]Android/FireStore QuerySnapshot convert to CustomObject

我目前programin測試QuizApp。 游戲玩法非常簡單我只想要一個問題的在線數據庫,用戶可以回答它們。

這就是數據庫的樣子:
在此輸入圖像描述

該集合問題包含唯一ID和名為“content”的自定義對象(questionObject)。 這個數字只是我可以查詢/搜索的簡單內容。

這是我的問題添加和查詢UI。 這只是一個小測試App。

public class questionAdder擴展AppCompatActivity {

EditText pQuestion, pAnwerA, pAnswerB, pAnswerC, pAnswerD, number;
Button pAdd, query;
private DatabaseReference databaseReference;
private FirebaseFirestore firebaseFirestore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addquestion);

    firebaseFirestore = FirebaseFirestore.getInstance();

    pQuestion = (EditText) findViewById(R.id.question);
    pAnwerA = (EditText) findViewById(R.id.answerA);
    pAnswerB = (EditText) findViewById(R.id.answerB);
    pAnswerC = (EditText) findViewById(R.id.answerC);
    pAnswerD = (EditText) findViewById(R.id.answerD);
    number = (EditText) findViewById(R.id.number);

    pAdd = (Button) findViewById(R.id.addQuestion);
    pAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            readQuestionStore();
        }
    });

    query = (Button) findViewById(R.id.query);
    query.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CollectionReference questionRef = firebaseFirestore.collection("questions");
            questionRef.whereEqualTo("content.number", "20").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    questionObject pContent = queryDocumentSnapshots.toObjects(questionObject.class);
                }
            });
        }
    });

}

public void readQuestionStore(){

    Map<String, Object> pContent = new HashMap<>();
    pContent.put("question", pQuestion.getText().toString());
    pContent.put("Corr Answer", pAnwerA.getText().toString());
    pContent.put("AnswerB", pAnswerB.getText().toString());
    pContent.put("AnswerC", pAnswerC.getText().toString());
    pContent.put("AnswerD", pAnswerD.getText().toString());
    questionObject content = new questionObject(pContent, number.getText().toString()); //document("Essen").collection("Katalog")

    firebaseFirestore.collection("questions").add(content).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
        @Override
        public void onSuccess(DocumentReference documentReference) {
            Toast.makeText(questionAdder.this, "Klappt", Toast.LENGTH_LONG).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(questionAdder.this, "Klappt nicht", Toast.LENGTH_LONG).show();
        }
    });
}

}



這就是我的questionObject的樣子:

public class questionObject {

private Map<String, Object> content;
private String number;

public questionObject(){

}

public questionObject(Map<String, Object> pContent, String pNumber) {
    this.content = pContent;
    this.number = pNumber;
}

public Map<String, Object> getContent() {
    return content;
}

public void setContent(Map<String, Object> content) {
    this.content = content;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

}



問題在onClickListener中的questionAdder類中我收到“不兼容類型”錯誤(找到:java.utils.list必需:questionObject)。

query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectionReference questionRef = firebaseFirestore.collection("questions");
                questionRef.whereEqualTo("content.number", "20").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        questionObject pContent = queryDocumentSnapshots.toObjects(questionObject.class);
                    }
                });
            }
        });

如果我將其更改為List,則為空。 所以實際的問題是,如何使用數據庫將CustomObject放入我的代碼中。 謝謝!

您收到此錯誤的原因是因為QuerySnapshot是一種“包含” 多個文檔的類型。 Firestore不會決定是否有一堆對象要返回,或者只返回一個。 這就是為什么你可以采取兩種不同的方法:

  1. 將數據放在custom object的列表中:

     List<questionObject> questionsList=new ArrayList<>(); if (!documentSnapshots.isEmpty()){ for (DocumentSnapshot snapshot:queryDocumentSnapshots) questionsList.add(snapshot.toObject(questionObject.class)); } 
  2. 如果您確定只獲得一個查詢對象,則可以從返回的queryDocumentSnapshots獲取第一個對象:

     questionObject object=queryDocumentSnapshots.getDocuments().get(0).toObject(questionObject.class); 

還有一些你應該注意的事情:

為什么要寫content.number而不僅僅是number 似乎number是問題document的單獨字段,因此您的代碼應如下所示:

CollectionReference questionRef = firebaseFirestore.collection("questions");
questionRef.whereEqualTo("number", "20").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        questionObject pContent = queryDocumentSnapshots.toObjects(questionObject.class);
    }
});

另外,嘗試將您的number字段更改為int ,因為它不是String而只是一個數字。

順便說一句,在開頭用大寫字母編寫類的名稱是更可接受的,例如: QuestionObject question=new QuestionObject();

暫無
暫無

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

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