Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not able to implement the filter in SearchView. (SearchView+ FirebaseRecyclerAdapter)

I am using the FirebaseRecyclerAdapter of Firebase-UI. I'm not able to implement the filter in SearchView.

My database:

enter image description here

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);
        }
}
like image 801
Rucha Bhatt Joshi Avatar asked Nov 07 '22 21:11

Rucha Bhatt Joshi


1 Answers

Implementing Perfect Search with Firebase Realtime Database

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.

An Alternative solution for your case

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.

like image 77
Ashutosh Barthwal Avatar answered Nov 14 '22 22:11

Ashutosh Barthwal