Android - Firebase searching with filtering

♀尐吖头ヾ 提交于 2019-12-13 02:49:44

问题


I'm developing an app that will help the user to search the books and download them which have been uploaded by other users as a form of the post.


In order to download the books first, the user should search them owing to attribute such as Title.


The posts which meet the requirement of the user should be displayed in the firebase-UI recyclerview.


This is my Firebase data structure:


I want the sample code that will create a Query which returns Id of convenient Posts such as ImgBRr2YFYfAu1WwENS1Ucj6jz13_1514813350760.


Please help me to solve the problem, any suggestions and ideas will be welcomed


回答1:


Here is what you can do,

For search by language and then filter by title & author

public void search(String languangeSearchString, String title, String author){  
        FirebaseDatabase.getInstance().getReference().child("Posts").equalTo("language").startAt(languangeSearchString).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                 String id = dataSnapshot.getKey();
                if(dataSnapshot.child("title").exists() && dataSnapshot.child("author").exists()){
                    if(dataSnapshot.child("title").getValue(String.class).equals(title) && dataSnapshot.child("author").getValue(String.class).equals(author)){
                        //Here are the results
                    }
                }           
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

}



回答2:


To perform searching on firebase-database, we commonly use orderBy(). Like

firebaseDatabase.getReference("Posts").child("postId")
                        .orderBy("authorId")

But it can't possible to use multiple orderBy() like,

firebaseDatabase.getReference("Posts").child("postId")
                     .orderBy("authorId")
                     .orderBy("title")     // this will throw an error
                     .orderBy("language") 

So you have to perform searching only on one child-node. For more info check this Query based on multiple where clauses in Firebase



来源:https://stackoverflow.com/questions/48061546/android-firebase-searching-with-filtering

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