Thomas Athanasiou
Thomas Athanasiou

Reputation: 121

How to filter FirebaseRecyclerOptions with SearchBar

I am building a fragment in which i have a searchBar for users and a recyclerView to display them. My question is how to change FirebaseRecyclerView Options every time i type something for searching, I found a solution with another adapter (not FirebaseRecyclerView Adapter) but it uses an ArrayList.

I want something like that for the options.

//userList contains all users
private void searchUser(String s) {
    ArrayList<User> list = new ArrayList<>();
    for(User object : userList){
        String userName = object.Name + " " + object.Surname;
        if(userName.toLowerCase().contains(s.toLowerCase())){
            list.add(object);
        }
    }
    UserAdapter adapter = new UserAdapter(list);
    myUserList.setAdapter(adapter);
}

How to refresh the options every time i type something on search Bar?

Upvotes: 0

Views: 418

Answers (2)

Thomas Athanasiou
Thomas Athanasiou

Reputation: 121

I needed a query indeed as @Black Mamba said. I used something like this:

Query query;
    if(!(searchTxt.isEmpty())) {
        query = Ref.orderByChild("Surname").startAt(search).endAt(search + "\uf8ff");
    }
    else{
        query = Ref;
    }

and the setOnQueryTextListener:

if (searchBar != null) {
        searchBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                firebaseSearch(newText);
                return true;
            }
        });

The firebaseSearch Method retrieves the data from the database.

Upvotes: 1

Black mamba
Black mamba

Reputation: 472

if you're familar with firebase recyclerview then firebase recyclerview gives that feature

firebase.database().ref(path).orderByChild().startAt('entered username');

startAt() is keyplayer for this... you have to adjust searchkey on usermodel class so that searchquery of firebase pick specified searcingword from tree of datanode.and later on set in firebase recyclerview

Upvotes: 1

Related Questions