问题描述
这是我的Firebase数据库结构
我想获取
类别 =猫"和级别 ="1"
category = "cat" and level = "1"
这是我的代码
Query query = firebaseReference.orderByChild("category").equalTo("cat1").limitToFirst(20);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UTILS.Log.e(TAG, "List Of User Count " + dataSnapshot.getChildrenCount());
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
//FCreateUSer modelRegister = dataSnapshot1.getValue(FCreateUSer.class);
UTILS.Log.e(TAG, "User Email :" + dataSnapshot1.child("question").getValue());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这很糟糕,但是如何在firbase中使用两个条件,我尝试了两个orderByChild
,但是给出了错误 java.lang.IllegalArgumentException:您无法合并多个orderBy调用!
it's wrok but how to use two condition in firbase i try two orderByChild
but it's give error java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!
推荐答案
观看此视频(使用JavaScript,但它确实解决了您的问题): https://youtu.be/sKFLI5FOOHs?t=612
Take a look at this video (it's in JavaScript, but it exactly solves your problem): https://youtu.be/sKFLI5FOOHs?t=612
基本上,您需要为查询构建数据.在OP示例中,问题将具有类似category_level: "cat_1"
的字段.然后,您可以执行查询equalTo("cat_1")
.如果需要其他查询,则可以省略category
和level
字段(但是在这种情况下,您将需要处理重复项).
Basically, you need to structure your data for your queries. In OP example Question will have field like category_level: "cat_1"
. Then you can perform query equalTo("cat_1")
. You can leave out both category
and level
fields if you need it for other queries (but you will need to handle duplication in that case).
如果您知道项目数量很少,最简单的解决方案是仅提取所有category=cat
并过滤level=1
项目.
If you know that your number of items is small, simplest solution is to just pull all category=cat
and filter level=1
items.
这篇关于如何在Android Firebase中应用多个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!