I have an adapter that is extended by ListAdapter, I use submit list to update the ui but when I use it when the user change the search query it displays other I items in the list however when I log the filtered list it is correct and the filtered items is correct.
Here is how I filter the list and submit it to the adapter :
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("")) {
refreshData();
} else {
List<Teacher> list = new ArrayList<>(originalList.stream().filter(item -> item.name.toLowerCase().trim().contains(newText.toLowerCase().trim())).collect(Collectors.toList()));
if (!teachersAdapter.getCurrentList().equals(list)) {
teachersAdapter.submitList(null);
teachersAdapter.submitList(list);
if (!teachersAdapter.getCurrentList().equals(list)) {
teachersAdapter.submitList(null);
teachersAdapter.submitList(list);
}
}
}
return false;
}
});
and this is the DiffUtil for the adapter
public TeachersAdapter(TeacherClicks teacherClicks) {
super(new DiffUtil.ItemCallback<Teacher>() {
@Override
public boolean areItemsTheSame(@NonNull Teacher oldItem, @NonNull Teacher newItem) {
return oldItem.id.equals(newItem.id);
}
@Override
public boolean areContentsTheSame(@NonNull Teacher oldItem, @NonNull Teacher newItem) {
return newItem.equals(oldItem) && newItem.toString().equals(oldItem.toString());
}
});
this.teacherClicks = teacherClicks;
}
This is the behavior resulted
I tried a lot of things but none of them works
If you are using LiveData to observe changes in your data make sure to update it on the main thread using postValue like this:
Also you dont need to call submitList(null) before submitting a new list, simply just use :
More, you need to simplify your areContentsTheSame method like this:
And lastly check if your "Teacher" class has a correct implementation of the equals method. The areItemsTheSame and areContentsTheSame methods in the DiffUtil.ItemCallback rely on this method.
This changes may help you to solve your problem.