android - 如何从 firebase 检索多个数据列表

标签 android firebase firebase-realtime-database

我一直在寻找不同的示例,但无法为我的 firebase 节点找到合适的解决方案。我有 3 个节点 1 个问题,第二个节点包含问题的答案,第三个节点包含对该答案的评论。!

我将如何能够使用 firebase 执行查询 我一直在寻找不同的例子,但无法为我的火基表找到合适的解决方案。我有 3 个节点

  1. 问题
  2. 答案
  3. 对答案的评论

我将如何能够使用 firebase 执行查询(基于连接的概念实现)

mdatabaseReference.child("Answer").equalTo(QID);

我将如何获得特定问题的答案以及对该答案的评论。!

这是我的 JSON

    {
      "Answer" : {

        "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518" : {

          "dateTime" : "16 Mar 2017 15:30:29",
          "professorAnswer" : "Hezbollah is an Islamist religious organization founded in 1985 and based in Lebanon. It follows Shi'Islam (also called Shi'ite Islam), the second largest denomination of Islam. Shi'a Muslims follow the teachings of the prophet Muhammad, a direct descendant of Isma'il (the first son of Ibrahim/Abraham).Contd.!",
          "professorId" : "7ceef713-eb59-4db4-a8d2-21d5a01eedfc",
          "questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2"
        }
      },

      "comment" : {
        "29192e3a-a013-4fcc-9859-1f5cc62464cb" : {
          "commentText" : "ORGANIZATION hezbollah bases on the bible but their goals is to save people in pagans work!",
          "dateTime" : "16 Mar 2017 15:30:52",
 "AnswerId" : "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518"
          "questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
          "userId" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
        }
      },



 "questions" : {

    "41c454a8-fab6-4e41-9093-b1120ffd1be0" : {
      "description" : "I know they're a Islamic organization but where are they based? What are their goals?",
      "idQuestion" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
      "time" : "16 Mar 2017 15:30:12",
      "title" : "What are the aims of the religious organization Hezbollah?",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2",

    }
  },



 "user" : {

    "13bd37e5-fc87-4468-a89e-7cb4ecaab05f" : {

      "email" : "email@gmail.com ",
      "user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
    }
}

问题是我想过滤那些事件...例如,使用 .orderByChild("Answer").equalTo(QID) 也许这是错误的查询,但它只是为了概念只获取给定问题 ID 的答案,然后用它填充我的列表。

最佳答案

正确的做法是分别获取每个数据,我不太清楚你的数据是如何组织的,但这可能是一个合适的解决方案:

// Gets the Question with id = QID
mdatabaseReference.child("Questions").child(QID);

// Gets the Answers for that question
mdatabaseReference.child("Answers").child(QID).once("value", function(answers) {

    // For every Answer gets the comments
    for(var answerID in answers) mdatabaseReference.child("Comments").child(QID).child(answerID);
});

编辑:要有效地使用 Firebase,您应该根据检索数据的方式构建数据。如果您想获得给定问题的所有答案,我建议您使用此数据结构:

{
    "Answers": {
        "questionID": {
            "answerID": {
                "dateTime" : "16 Mar 2017 15:30:29",
                "professorAnswer" : "Hezbollah is an Islamist religious...,
                "professorID" : "...",
                "questionID" : "..."
            }
        }
    }
}

这样您就可以通过这种方式获取给定问题 ID 的数据:

mdatabaseReference.child("Answer").child(questionID).once('value', function(answers) {
    // answers contains all the answer for the question with ID == questionID
});

注意:您在 Firebase 中没有表格,一切都是 JSON 对象

关于android - 如何从 firebase 检索多个数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832896/

相关文章:

java - 是否可以创建 ByteArrays 的 ByteArray 作为 kotlin 中的元素?

Android Studio - 为所有模块添加依赖

ios - 快速解码 httpsCallable 云函数响应

android - 使用 Firebase 从列表中查询每个节点的键

android - 是否有必要从 Firebase 实时数据库中删除每个添加的监听器?

java - 来自android示例的TimePicker不起作用

Android id命名约定: lower case with underscore vs.驼峰式

swift - 从当前用户 firebase swift 获取文档

swift - 快速从 firebase 数据库中读取子值

android - 使用 Firebase 填充小部件