Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android/FireStore查询并返回自定义对象_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Android/FireStore查询并返回自定义对象

Android/FireStore查询并返回自定义对象,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我还有一个关于Firestore和Android/Java实现的问题,但这次是关于代码。这就是我的数据库的样子: 它将是一个QuizApp,数据库包含一个生成的ID和一个customObject(类型:questionDataObject name:content)还有一个数组列表,其中包含以下限制/思想: [0]:问题 :正确答案 [2] …[4]回答错误 我在questionDataObject中添加了一个字符串“number”,以便于搜索/查询。这就是我的问题,我无法让查询正常工作。

我还有一个关于Firestore和Android/Java实现的问题,但这次是关于代码。这就是我的数据库的样子:



它将是一个QuizApp,数据库包含一个生成的
ID
和一个
customObject(类型:questionDataObject name:content)
还有一个数组列表,其中包含以下限制/思想:

[0]:问题
:正确答案
[2] …[4]回答错误

我在questionDataObject中添加了一个字符串“number”,以便于搜索/查询。这就是我的问题,我无法让查询正常工作。

    public class questionAdder extends 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");
                com.google.firebase.firestore.Query query = questionRef.whereEqualTo("content.number", "20");
                query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        //questionObject content = queryDocumentSnapshots.toObjects(questionObject.class);
                    }
                });
            }
        });
    }

    public void readQuestionStore(){
        ArrayList<String> pContent = new ArrayList<>();
        pContent.add(0, pQuestion.getText().toString());
        pContent.add(1, pAnwerA.getText().toString());
        pContent.add(2, pAnswerB.getText().toString());
        pContent.add(3, pAnswerC.getText().toString());
        pContent.add(4, 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();
            }
        });
    }
}
公共类问题加法器扩展AppCompative活动{
编辑文本pQuestion,pAnwerA,pAnswerB,pAnswerC,pAnswerD,编号;
按钮填充,查询;
私有数据库参考数据库参考;
私人FirebaseFirestore FirebaseFirestore;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.addquestion);
firebaseFirestore=firebaseFirestore.getInstance();
pQuestion=(编辑文本)findviewbyd(R.id.question);
pAnwerA=(EditText)findViewById(R.id.answerA);
pAnswerB=(EditText)findViewById(R.id.answerB);
pAnswerC=(编辑文本)findviewbyd(R.id.answerC);
pAnswerD=(编辑文本)findViewById(R.id.answerD);
number=(EditText)findViewById(R.id.number);
pAdd=(按钮)findViewById(R.id.addQuestion);
pAdd.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
readQuestionStore();
}
});
query=(按钮)findviewbyd(R.id.query);
query.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
CollectionReference questionRef=firebaseFirestore.collection(“问题”);
com.google.firebase.firestore.Query Query=questionRef.whereEqualTo(“content.number”,“20”);
query.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(QuerySnapshot QueryDocumentSnapshot){
//questionObject内容=queryDocumentSnapshots.ToObject(questionObject.class);
}
});
}
});
}
公共无效readQuestionStore(){
ArrayList pContent=新的ArrayList();
添加(0,pQuestion.getText().toString());
添加(1,pAnwerA.getText().toString());
添加(2,pAnswerB.getText().toString());
添加(3,pAnswerC.getText().toString());
添加(4,pAnswerD.getText().toString());
questionObject内容=新的questionObject(pContent,number.getText().toString());//文档(“Essen”).collection(“Katalog”)
firebaseFirestore.collection(“questions”).add(内容).addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(文档引用文档引用){
Toast.makeText(questionAdder.this,“Klappt”,Toast.LENGTH_LONG.show();
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Toast.makeText(questionAdder.this,“Klappt nicht”,Toast.LENGTH_LONG.show();
}
});
}
}

公共类问题对象{
私有ArrayList内容;
私有字符串编号;
公共对象(){
}
公共问题对象(ArrayList pContent,字符串pNumber){
this.content=p内容;
this.number=pNumber;
}
公共ArrayList getContent(){
返回内容;
}
公共无效集合内容(ArrayList内容){
this.content=内容;
}
公共字符串getNumber(){
返回号码;
}
公共无效集合号(字符串号){
这个数字=数字;
}
}

这个应用程序不是我想发布的,我只是想练习Firebase Firestore编码等。

问题:如何从数据库获取对象,如何检查查询是否成功?我实际上看不出查询是否找到了我的条目。我得到的唯一反馈是云引擎添加了一个“read”


谢谢大家!

首先,我建议您仔细阅读,这将指导您从
云Firestore
数据库获取数据

如果您跳到自定义对象部分,您将看到以下代码:

DocumentReference docRef = db.collection("cities").document("BJ");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        City city = documentSnapshot.toObject(City.class);
    }
});
CollectionReference questionRef = firebaseFirestore.collection("questions");
questionRef.whereEqualTo("content.number", "20").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

    }
});
这就是为什么您的
内容
属性应该如下所示:

Map<Integer, Object> content = new HashMap<>();
content.put(0, "a");
content.put(1, "a");
content.put(2, "a");
content.put(3, "a");

谢谢你的快速回复。我已经找到了获取数据的部分,但我不确定如何将查询与之结合起来。我在按钮查询的点击监听器中有这段代码,理论上正确吗?我划掉了.toObject部分,因为我确实收到了一个ArrayList不兼容的错误。我将删除数组并添加一个hashmap作为第一步!是的,将您的侦听器置于
OnClickListener
是一种选择。如果有更多代码要执行,它也可以在单独的
方法中执行。请确保在请求开始时显示进度条,并在请求结束时禁用进度条,以便用户了解其状态。哦,对不起,我描述错了。我想询问OnClickListener中的query和get命令是否正确。Firebase文档只描述了Get only或Query only示例,但我想查询并获取。对不起,我不是母语人士,我希望我现在可以更好地描述它。@J.Lo我已经编辑了我的答案,并添加了查询和Get的组合版本。我希望它能回答你的问题
Map<Integer, Object> content = new HashMap<>();
content.put(0, "a");
content.put(1, "a");
content.put(2, "a");
content.put(3, "a");
CollectionReference questionRef = firebaseFirestore.collection("questions");
questionRef.whereEqualTo("content.number", "20").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

    }
});