Search code examples
androidfirebasefirebase-realtime-databasesearchviewfirebaseui

Firebase searching by character


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:

enter image description here

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:

enter image description here

And here's what I got when I write "M" or "m":

enter image description here

So I only need >> When I write "m" automatically all users name start with "m" must be shown like mike.


Solution

  • Try this

    Query Q = mDatabase.child("Users").orderByChild("Username").startAt(newText).endAt("~");