0

I'm implementing Filter in my Custom Adapter but I got CME when I press the search icon in action bar. I do some research and edit my code but no effect.

My code:

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

What can I do?

Minh Phan
  • 47
  • 6
  • 1
    Post full stack trace. – nKn Apr 24 '14 at 10:30
  • 1
    what is add(f) method? please post that too – drulabs Apr 24 '14 at 10:39
  • It's just the add() method in ArrayAdapter – Minh Phan Apr 24 '14 at 16:35
  • Does the `CME` occur always? Are you using `Thread`s somewhere? – nKn Apr 24 '14 at 16:51
  • yes, every time I press the search icon in action bar. I implemented swipe tab in my app that has 1 activity contains 2 fragments, each fragment has one AsyncTaskLoader to load data. CME didn't occurr until I implemented Filter – Minh Phan Apr 25 '14 at 06:52
  • There's something rare, I copied your code into a project of mine, filled a `List` with a few instances of a class, and made a `for` loop like yours and doesn't throw a `CME`. It shouldn't, indeed, as you're not modifying anything. Could you post your full Stack Trace and double-check if the line you pointed is the one that actually throws the `CME`? – nKn Apr 25 '14 at 07:31
  • Possible duplicate of [ConcurrentModificationException inside PublishResult - ArrayAdapter](https://stackoverflow.com/questions/41316886/concurrentmodificationexception-inside-publishresult-arrayadapter) – Alireza Noorali Jan 09 '19 at 11:25

0 Answers0