I have written a customer filter inside my adapter to allow a user to search by a customer's name. I followed the answers provided on this question, specifically the answer with 35 up-votes, as the selected answer modifies the original list and causes errors. The filtering works correctly, but if you press backspace after searching for a customer name, the results do not update. Here is my filter method, thanks for any help beforehand.
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<CustomerView> suggestions = new ArrayList<>();
if (constraint != null) {
suggestions.clear();
for (CustomerView customer : customers) {
if (customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(customer);
}
}
results.values = suggestions;
results.count = suggestions.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
if (results != null && results.count > 0) {
addAll((ArrayList<CustomerView>) results.values);
} else {
addAll(customers);
}
notifyDataSetChanged();
}
};
}