简体   繁体   English

如何从firebase检索特定的数据列表?

[英]How to retrieve specific list of data from firebase?

Is there any possible way to get result from child of child.有没有办法从孩子的孩子那里得到结果。 I'm looking for multiple answers on a question.!我正在寻找一个问题的多个答案。!

"questions" : {
    "Q1(Random ID )" : {
      "description" : "Deail of Question",
      "idQuestion" : "Q1",
      "time" : "17 Mar 2017 16:18:12",
      "title" : "Title Of Question",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
    },
{
  "Answer" : {
    "81d19e85-8c6c-4824-9fb1-61a7cd316e32" : {
      "dateTime" : "17 Mar 2017 16:22:17",
      "Answer" : "Answer",
      "ProfessorId" : "da587c89-f001-43f2-984d-e6dbac491acc",
      "questionId" : "Q1"
    },
    "e2ec0556-4420-4d2f-b346-0b5cebeb1ebe" : {
      "dateTime" : "17 Mar 2017 16:55:27",
      "Answer" : "Answer 2 on Question 1",
      "answerId" : "0d20d6df-907d-4133-be53-ca6acf6c2ad1",
      "questionId" : "Q1"
    }
  },

nodes that have been generated randomly.随机生成的节点。 ! want to do something like this .!想做这样的事情.!

mdatabaseReference.child("Answer").child(* Here Im Using "*" Like we used to do in SQl to get all data I knw its not a SQL but its just for conecpt).equalTo(QID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (Answer_List.size() > 0)
                    Answer_List.clear();


                for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {

Instead of using using postSnapshot.getKey().equals("QID")而不是使用postSnapshot.getKey().equals("QID")

    if(postSnapshot.getKey().equals("QID"))  
 Answertext = postSnapshot.getValue().toString();

It will make performance slower I'm looking for some thing faster.!它会使性能变慢我正在寻找更快的东西。!

looking for something like this寻找这样的东西

 mdatabaseReference.child("Answer").child("*").getKey().equals(QID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

You're looking for Firebase Database queries .您正在寻找Firebase 数据库查询

mdatabaseReference
  .child("Answer")
  .orderByChild("questionId")
  .equalTo("Q1")
  .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      for (DataSnapshot answerSnapshot: dataSnapshot.getChildren()) {
        System.out.println(answerSnapshot.child("Answer").getValue());
      }
    }
    ...

However, while this will work, I highly recommend that you change your data model to make this type of query more scaleable.然而,虽然这会奏效,但我强烈建议您更改数据模型以使此类查询更具可扩展性。 Since answers fall under questions, you should model that hierarchy.由于答案属于问题,您应该对该层次结构进行建模。 While you should not mix questions and answers, you should consider modeling the answers under the question ID:虽然您不应该混合问题和答案,但您应该考虑在问题 ID 下对答案进行建模:

"questions" : {
    "Q1(Random ID )" : {
      "description" : "Deail of Question",
      "idQuestion" : "Q1",
      "time" : "17 Mar 2017 16:18:12",
      "title" : "Title Of Question",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
    },
}
"Answer" : {
  "Q1": {
    "81d19e85-8c6c-4824-9fb1-61a7cd316e32" : {
      "dateTime" : "17 Mar 2017 16:22:17",
      "Answer" : "Answer",
      "ProfessorId" : "da587c89-f001-43f2-984d-e6dbac491acc",
    },
    "e2ec0556-4420-4d2f-b346-0b5cebeb1ebe" : {
      "dateTime" : "17 Mar 2017 16:55:27",
      "Answer" : "Answer 2 on Question 1",
      "answerId" : "0d20d6df-907d-4133-be53-ca6acf6c2ad1",
    }
  }
}

With this structure you can get the answers for Q1 with a direct lookup instead of a query (on an every growing list):使用此结构,您可以通过直接查找而不是查询(在每个不断增长的列表中)获得 Q1 的答案:

mdatabaseReference
  .child("Answer")
  .child("Q1")
  .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      for (DataSnapshot answerSnapshot: dataSnapshot.getChildren()) {
        System.out.println(answerSnapshot.child("Answer").getValue());
      }
    }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM