I've implemented filter function inside my adapter, with DiffUtil.
As for now - the filter works, the problem is that if I remove chars from the search, It's just return empty list.
How can I solve this problem?
my filter function:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Cars> listResult;
if (constraint.length() == 0) {
listResult = getCurrentList();
} else {
listResult = getFilteredResults(constraint.toString().toLowerCase());
}
FilterResults results = new FilterResults();
results.values = listResult;
Log.i("filter", "results results.values" + results.values);
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.i("filter", "publishResults started");
submitList((List<Cars>)results.values);
}
};
}
getFilterResults:
private List<Cars> getFilteredResults(String constraint) {
List<Cars> results = new ArrayList<>();
for (Cars item : getCurrentList()) {
if (item.getTitle().toLowerCase().contains(constraint)) {
results.add(item);
}
}
return results;
}
UPDATE: i might have been spotted the issue - I've newList variable the that suppose to save the original list. I'm initilaze this in the constructor like this:
this.newList = getCurrentList()
but getCurrentList() return empty list.