Reputation: 370
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
Upvotes: 1
Views: 3024
Reputation: 1167
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) {
}
});
}
Upvotes: 3
Reputation: 3673
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
Upvotes: 1