I am using the FirebaseRecyclerAdapter of Firebase-UI. I'm not able to implement the filter in SearchView.
My database:
My Question:
How to search in FirebaseDatabase by character using FirebaseRecyclerAdapter ?
(I want to search by username)
My Code: I used this method for search, what's wrong in my code?
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Query Q = mAllUsersDatabaseRef.child("User").orderByChild("username").startAt(newText).endAt("~");
FirebaseRecyclerAdapter<User, SearchFeedFragment.AllRegisterUserViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, SearchFeedFragment.AllRegisterUserViewHolder>(
User.class, R.layout.row_search_all_users, SearchFeedFragment.AllRegisterUserViewHolder.class, Q) {
@Override
protected void populateViewHolder( SearchFeedFragment.AllRegisterUserViewHolder viewHolder,User model, int position) {
viewHolder.setUserName(model.getUsername());
viewHolder.setUserProfilePic(Common.stringToBitMap(model.getProfilepicurl()));
}
};
mRecyclerViewSearch.setAdapter(firebaseRecyclerAdapter);
return false;
}
});
public static class AllRegisterUserViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView mImgProfileImage = (ImageView) itemView.findViewById(R.id.iv_searchProfilePic);
TextView mTxtProfileName = (TextView) itemView.findViewById(R.id.txt_searchProfileName);
public AllRegisterUserViewHolder(View itemView) {
super(itemView);
itemView = mView;
}
public void setUserName(String username) {
mTxtProfileName.setText(username);
}
public void setUserProfilePic(Bitmap image) {
mImgProfileImage.setImageBitmap(image);
}
}
Queries, Part 2: Advanced Searches with Firebase, made Plug-and-Play Simple
Read this and you will learn why and how about Firebase Realtime Database Search and how to implement a search Query.
you need to update one line of your code.
Query Q = mAllUsersDatabaseRef.child("User").orderByChild("username").startAt(newText).endAt(newText+"\uf8ff");
this will work for matching from start only. Also you can learn more about startAt(),endAt(),equalTo(),limitToFirst() and limitToLast() they will help you make your search query better without using any external resource other than firebase.
also i hope there is no issues with the Firebase Realtime Database Rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With