问题
I am using FirebaseRecyclerAdapter in my firebase app and I am still till now didn't know how to search in Firebase by character, I have already used an query and I have got a good result but its not usable, here's my database I want to search in:
And here's my method that I use it but it get the "Username" if I enter full name only so if I write "m" nothing show:
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;
}
});
Here's what I got when I enter fully name:
And here's what I got when I write "M" or "m":
So I only need >> When I write "m" automatically all users name start with "m" must be shown like mike.
回答1:
Try this
Query Q = mDatabase.child("Users").orderByChild("Username").startAt(newText).endAt("~");
回答2:
It happens because you are using:
Query Q = mDatabase.child("Users").orderByChild("Username").equalTo(newText);
The equalsTo creates a query constrained to only return child nodes with the given value.
You should use the method startAt which creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.
来源:https://stackoverflow.com/questions/42536273/firebase-searching-by-character