I have worked for search about how to search in firebase by vaule and I couldn't have a fully answer so I am asking now ...
I have used this method and I couldn't get the correct result:
I need to search by "Username".
Here's my database:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mQuery = mDatabase.orderByChild("Username");
return false;
}
});
And also I have used:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mQuery = mDatabase.orderByChild("Username").equalTo(newText)
return false;
}
});
but I couldn't so can anyone give me fully correct answer please
Thanks AtaerCaner for helping me and also thanks for trying to help me as much as you can, but I still haven't got any result from the Search View so I will give the method that is in my class:
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
String A = (String) dataSnapshot.child(Post_Key).child("Username").getValue();
mQuery = mDatabase.orderByChild(A).equalTo(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String A = (String) dataSnapshot.child(Post_Key).child("Username").getValue();
mQuery = mDatabase.orderByChild(A).equalTo(newText);
return false;
}
});
}
So Please check the method if its correct or not ..
Let's think i'm searching Mike. First, bring all users by username with mDatabase.child("Users").orderByChild("Username")
. But i want the Mike's datas, so i add .equalTo("Mike")
. This returns a query as i say then set an .addListenerForSingleValueEvent
to get datas of Mike. The information i want will come as iterable object with one item in this callback. Then cast it to your User modal.
mDatabase.child("Users").orderByChild("Username").equalTo("Mike").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getChildren().iterator().next().getValue(User.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
As far as i see in your code you get the query with mQuery = mDatabase.orderByChild(A).equalTo(newText)
but you don't do anything with it.
Maybe this guy can help you https://www.youtube.com/watch?v=3WTQZV5-roY