gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:39:03 24 4
gpt4 key购买 nike

有没有可能从 child 的 child 那里得到结果。我正在寻找一个问题的多个答案。!

"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"
}
},

随机生成的节点。 !想做这样的事情.!

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()) {

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

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

它会使性能变慢我正在寻找更快的东西。!

寻找这样的东西

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

最佳答案

您正在寻找 Firebase Database queries .

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());
}
}
...

但是,虽然这可行,但我强烈建议您更改数据模型以使此类查询更具可扩展性。由于答案属于问题,您应该为该层次结构建模。虽然您不应混合问题和答案,但您应该考虑在问题 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",
}
}
}

使用这种结构,您可以通过直接查找而不是查询(在每个增长的列表上)获得 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());
}
}
...

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com