publishResults中的并发修改异常

时间:2014-04-24 10:29:11

标签: android concurrentmodification

我在自定义适配器中实现过滤器,但是当我按下操作栏中的搜索图标时,我获得了CME。我做了一些研究和编辑我的代码,但没有效果。

我的代码:

private class TaskFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<TaskModel> filteredItems = new ArrayList<TaskModel>();

                for (int i = 0, l = originalList.size(); i < l; i++) {
                    TaskModel country = originalList.get(i);
                    if (country.getName().toString().toLowerCase().contains(constraint))
                        filteredItems.add(country);
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            } else {
                synchronized (this) {
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            clear();
            List<TaskModel> listcopy = (List<TaskModel>) results.values;
            synchronized (listcopy) {
                for (Iterator<TaskModel> it = listcopy.iterator(); it.hasNext(); ) {  
                    TaskModel f = (TaskModel) it.next();  //this one cause the CME
                    add(f);
                }  
            }
            notifyDataSetChanged();
        }
    }

我该怎么办?

0 个答案:

没有答案