Filtering causes both lists to delete their Items - Android RecyclerView Filtering Java

159 views Asked by At

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!

1

There are 1 answers

5
dongnez On

On publish Result you are clearing the List, i think that is wrong:

 @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        //stellplaetze.clear();
        stellplaetze.addAll((ArrayList) results.values);
        notifyDataSetChanged();
    }

I have an example of my code i think this will help:

First i create 2 list:

private ArrayList<CountryPhone> objects;
private ArrayList<CountryPhone> filteredData;

 @Override
public int getCount() {
    return filteredData.size();
}

@Nullable
@Override
public CountryPhone getItem(int position) {
    return filteredData.get(position);
}

On Filter Results:

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();
        FilterResults filterResults = new FilterResults();
        ArrayList<CountryPhone> filterlist = new ArrayList<>();

            String name, code;
            for (int i = 0; i < objects.size(); i++) {
                CountryPhone item = objects.get(i);
                name = item.getName().toLowerCase();

                code = item.getCode();

                if (name.contains(filterString) || code.contains(filterString)){
                    filterlist.add(item);
                }
            }

            filterResults.values = filterlist;
            filterResults.count = filterlist.size();

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<CountryPhone>) results.values;

            notifyDataSetChanged();

    }

};