Search Firebase by character
I am using FirebaseRecyclerAdapter in my firebase application and I still don’t know how to search in Firebase by character, I have already used query and I have good result but it cannot be used, here is my database I want to search in:
And here is my method that I am using, but it gets "Username" if I enter the full name, only if I write "m" show nothing:
mMainSearcher.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Query Q = mDatabase.child("Users").orderByChild("Username").equalTo(newText);
FirebaseRecyclerAdapter<Getting_Friends, Search.SearchViewHolder> firebaseRecyclerAdapter22 = new FirebaseRecyclerAdapter<Getting_Friends, Search.SearchViewHolder>(
Getting_Friends.class, R.layout.my_friends_card, Search.SearchViewHolder.class, Q) {
@Override
protected void populateViewHolder(final Search.SearchViewHolder viewHolder, final Getting_Friends model, int position) {
viewHolder.setUsername(model.getUsername());
viewHolder.setProfile(getApplicationContext(), model.getProfile());
}
};
mFriendsRecyclerView.setAdapter(firebaseRecyclerAdapter22);
return false;
}
});
This is what I got when I entered the full name:
And this is what I got when I write "M" or "m":
So, I only need -> When I write "m" automatically, all usernames start with "m" should display as mike.
source to share
This is because you are using:
Query Q = mDatabase.child("Users").orderByChild("Username").equalTo(newText);
equalsTo
creates a request limited to only returning child nodes with a given value.
You should use a method startAt
that creates a request limited to only returning child nodes with a value greater than or equal to the specified value, using the specified directive or the default orderBy priority.
source to share