Firebase orderByChild和equalTo()查询不起作用 [英] Firebase orderByChild and equalTo() queries don't work
问题描述
我使用以下Firebase数据库表开发Android应用程式:
posts:{
id_1:{
author:google:111527135678918251124,
color:-2960686,
creationTime:1427104145195,
text:my文本,
title:my Title,
type:0,
visible:true
},
id_2:{
author:google:111527135678918251524,
color:-2960686,
creationTime:1427104145195,
text:my text,
title:my Title,
type:2,
visible:true
},
id_3:{
作者:google:111527135678918251124,
color:-2960686,
creationTime:1427104145195,
text:我的文本,
title :my Title,
type:1,
visible:true
}
}
我希望能够检索由孩子排序的帖子(按类型排序为例),并能够检索具有特定子值(类型= 1)。
阅读Firebase文档,似乎我必须写下面的代码,但是我没有得到想要的结果。
按类型排序
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ root
$ b @Override
public void onDataChange(DataSnapshot dataSnapshot){
result =(HashMap< String,Post>)dataSnapshot.getValue();
}
@Override
public void onCancelled(FirebaseError firebaseError){
});
我得到了:一个无序的帖子图
Type = value
rootRef.child(posts)。orderByChild(type)。equalTo(1,类型)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
result = dataSnapshot.getValue();
result = result;
$ b @Override
public void onCancelled(FirebaseError firebaseError){
})
我得到: null
。
$ Datasnapshot
object has key =posts
and value = null
关于排序,我认为问题是我检索数据为 HashMap
本身是无序的。
正如注释中所建议的,第二种情况需要 .equalTo 1)代替
.equalTo(type,1)
,因为它的前面是 .orderBy() code>方法。
I'm developing an Android app with the following Firebase database table:
"posts": {
"id_1": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 0,
"visible": true
},
"id_2": {
"author": "google:111527135678918251524",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 2,
"visible": true
},
"id_3": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 1,
"visible": true
}
}
I'd like to be able to retrieve a the posts sorted by a child (sorted by type as an example) and be able to retrieve all the posts with a specific child value (type = 1). Reading the Firebase docs it seems I have to write the following code, but I don't get the wanted result.
Order by type
rootRef.child("posts").orderByChild("type")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = (HashMap<String, Post>) dataSnapshot.getValue();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
I get: an unordered Map of Posts
Type = value
rootRef.child("posts").orderByChild("type").equalTo(1, "type").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = dataSnapshot.getValue();
result = result;
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
})
I get: null
.
My Datasnapshot
object has key = "posts"
and value = null
About the ordering, I think the problem is that I'm retrieving data as a HashMap
which is itself unordered.
As suggested in the comments the second case needs .equalTo(1)
instead of .equalTo("type",1)
because it's preceded by the .orderBy()
method.
这篇关于Firebase orderByChild和equalTo()查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!