如果我们没有结果,如何清理过滤后的列表视图

How to clean a filtered listview if we haven't results

我做了一个方法来用 EditText 中的文本过滤 ListView。我的问题是我把chars放在edittext中,有一段时间没有结果,但我不知道如何清理listview。

我的文字观察者:

    search_watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (count < before) {
                        // We're deleting char so we need to reset the adapter data
                        adapter.resetData();
                    }

                    adapter.getFilter().filter(s.toString());
...

适配器是:

private class FarmacoFilter extends Filter {



        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic

            String str_search = constraint.toString();

            if (str_search == null || str_search.length() == 0) {
                // No filter implemented we return all the list
                results.values = farmList;
                results.count = farmList.size();
            }
            else {
                // We perform filtering operation
                List<PojoFarmaco> nFarmList = new ArrayList<PojoFarmaco>();

                for (PojoFarmaco p : farmList) {
                    if (p.getName().toUpperCase().contains(str_search.toUpperCase()) || p.getAmpolla().toUpperCase().contains(str_search.toUpperCase()))
                        nFarmList.add(p);
                }

                results.values = nFarmList;
                results.count = nFarmList.size();

            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                farmList = (List<PojoFarmaco>) results.values;
                notifyDataSetChanged();
            }

        }

    }

示例:

如果我写 "GOO",我会找到 "GOOGLE" 和其他结果,但是如果我继续写更多的字符,比如 "GOOZZZ",我会找到最新的结果。

我需要做什么?谢谢你的建议。

你应该保持原始数据集完好无损。在这一刻,你正在这一行中覆盖它

farmList = (List<PojoFarmaco>) results.values;

如果我们将 farmListOriginal 称为原始数据集并将 farmList 称为它的副本,您可以像这样轻松修复它

 if (results.count == 0)
     farmList = new ArrayList<>(farmListOrig);
 else {
     farmList = (List<PojoFarmaco>) results.values;      
 }
 notifyDataSetChanged();

我的假设是 farmList 是一个 ArrayList

解决方案是编辑 getFilter 的 publishResults:

@Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0){
                resetData();
                farmList = (List<PojoFarmaco>) results.values;
            }
            else {
                farmList = (List<PojoFarmaco>) results.values;
            }
            notifyDataSetChanged();
        }

和 Blackbelt 说的一样的解决方案,但是我有 resetData 方法来恢复到原始数据。