I'm using the Cloud Firestore of Firebase to store users and the android Searchview to provide search fonctionnality. When a user search for "jonathan" for example, if he begin typing "j" i want to bring in all users with the name starting par "j". How can i achieve this ?
Here is what i have tried:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});
return true;
}
Getting the user from Firebase:
private void searchUsers(String recherche) {
if(recherche.length() > 0)
recherche = recherche.substring(0,1).toUpperCase() + recherche.substring(1).toLowerCase();
listUsers = new ArrayList<>();
db.collection("users").whereGreaterThanOrEqualTo("name", recherche)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
System.err.println("Listen failed:" + e);
return;
}
listUsers = new ArrayList<User>();
for (DocumentSnapshot doc : snapshots) {
User user = doc.toObject(User.class);
listUsers.add(user);
}
updateListUsers(listUsers);
}
});
}
This works only for the first letter "j" as soon as i add "ja" for example i still have all the "jonathan" displayed