[英]How to retrieve specific list of data from firebase?
有沒有辦法從孩子的孩子那里得到結果。 我正在尋找一個問題的多個答案。!
"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 數據庫查詢。
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());
}
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.