Android / FireStore QuerySnapshot转换为CustomObject

时间:2018-04-01 15:21:47

标签: java android firebase google-cloud-firestore

我目前正在测试QuizApp。游戏玩法非常简单我只想要一个问题的在线数据库,用户可以回答它们。

这就是数据库的样子:
enter image description here

该集合问题包含唯一ID和名为' content'的自定义对象(questionObject)。这个数字只是我可以查询/搜索的简单内容。

这是我的问题添加和查询UI。它只是一个小测试应用程序。

    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中的那个问题加法器类中,我收到了一个&#34;不兼容的类型&#34;错误(找到: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放入我的代码中。谢谢!

1 个答案:

答案 0 :(得分:4)

您收到此错误的原因是因为QuerySnapshot是一种&#34;包含&#34; 多个文件。 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);
    
  3. 你应该注意的一些事情:

    为什么要写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();