So what i want to do is simply filter all of my entries according to a searchString. This works fine but for some reason everytime i filter both lists delete the items. i will only include the (i think) important parts of code here.
Adapter.java:
@Override
public Filter getFilter() {
return myFilter;
}
private Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Stellplatz> filteredList = new ArrayList<>();
Log.d("TEst","Size stellplaetzeall:"+ stellplaetzeall.size());
Log.d("TEst","Size Stellplaeztze:"+ stellplaetze.size());
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(stellplaetzeall);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Stellplatz item : stellplaetzeall) {
if (item.getAnbieter().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
stellplaetze.clear();
stellplaetze.addAll((ArrayList) results.values);
notifyDataSetChanged();
}
};
Fragment.java
@Override
public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater){
inflater.inflate(R.menu.menu_itemlist, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
I cant see why this would happen. Every example i found is basically the same as my code but for some reason it happens. i would really appreciate any answer that helps me understand this. also im not very good at android programming, first time doing this so an explanation why would be really helpful!
On publish Result you are clearing the List, i think that is wrong:
I have an example of my code i think this will help:
First i create 2 list:
On Filter Results: