0

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?

Thomas Athanasiou
  • 121
  • 1
  • 2
  • 11
  • Check **[this](https://stackoverflow.com/questions/50682046/applying-word-stemming-in-searchview-for-fetch-data-from-firebase-database/50682657)** out. – Alex Mamo Jun 06 '19 at 13:15

2 Answers2

1

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

Black mamba
  • 462
  • 4
  • 15
  • How to add this on FirebaseRecycler options? Do i set a new Query? – Thomas Athanasiou Jun 11 '19 at 15:11
  • 1
    yes you need to set new query on every filter request for more detail you can watch this https://www.youtube.com/watch?v=6h-A3sVIed0&list=PLxefhmF0pcPlqmH_VfWneUjfuqhreUz-O&index=34 – Black mamba Jun 12 '19 at 06:49
1

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.

Thomas Athanasiou
  • 121
  • 1
  • 2
  • 11