Skip to content

Instantly share code, notes, and snippets.

@muth0mi
Created August 8, 2019 17:55
Show Gist options
  • Select an option

  • Save muth0mi/b42612b671caa06558688c4efe1cb921 to your computer and use it in GitHub Desktop.

Select an option

Save muth0mi/b42612b671caa06558688c4efe1cb921 to your computer and use it in GitHub Desktop.
Search Implementation
private List<T> showingList = new ArrayList<>(); // showingList is the collection used by the recyclerView adapter
// Method implementing search
public void performSearch(String query) {
/* ToDo: Show loading progress bars */
if (!query.isEmpty()) {
// For holding search results
List<T> results = new ArrayList<>();
// Loop showing list looking for matches
for (T t : showingList){
// Add any results matching criteria to results list
if (t.toString().toLowerCase().contains(query)) {
results.add(t);
}
}
// Check if there are not results found
if (results.isEmpty()){
/* ToDo: Show 'No results found' prompt */
/* ToDo: Hide any loading progress bars */
}
// Set results to the recyclerView adapter and reload
else {
showingList = results;
notifyDataSetChanged();
}
}
}
@muth0mi
Copy link
Author

muth0mi commented Aug 8, 2019

SearchView sv ; // ....

sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@OverRide
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    adapter.performSearch(newText.toString().trim().toLowerCase()); // adapter is your recyclerView adapter (where the above method goes btw)
    return false;
}

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment