How to apply multiple query in android firebase? [duplicate]

心已入冬 提交于 2019-12-24 01:54:33

问题


This is My Firebase database structure

i want to get data like

category = "cat" and level = "1"

Here is my code

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

            }

        });

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!


回答1:


Take a look at this video (it's in JavaScript, but it exactly solves your problem): https://youtu.be/sKFLI5FOOHs?t=612

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).

If you know that your number of items is small, simplest solution is to just pull all category=cat and filter level=1 items.



来源:https://stackoverflow.com/questions/41262660/how-to-apply-multiple-query-in-android-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!