简体   繁体   English

过滤导致两个列表都删除它们的项目 - Android RecyclerView 过滤 Java

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

So what i want to do is simply filter all of my entries according to a searchString.所以我想做的只是根据 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:适配器.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片段.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!我也不太擅长 android 编程,第一次这样做所以解释为什么会很有帮助!

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:首先我创建 2 个列表:

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();

    }

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM