Before I was getting Concurrent Modification Exception. I avoided it, by creating a new arraylist. But, now I'm getting a Null pointer exception & strange thing is that I've done all neccessary null checks. Still I don't understand What might went wrong.
Here is my publishResults() method:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
mSuggestions.clear();
for (Item item : mItemsAll) {
try {
if (item.name.toLowerCase(Locale.US).startsWith(constraint.toString().toLowerCase(Locale.US))) {
mSuggestions.add(item);
}
} catch (Exception ex) {
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = mSuggestions;
filterResults.count = mSuggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
Here mItemsAll holds the copy of the mainList given to the adapter
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
@SuppressWarnings("unchecked")
ArrayList<Item> filteredList = (ArrayList<Item>) results.values;
if (results != null && results.count > 0) {
clear();
for (Item c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};
results contains the filtered results obtained from the `performFiltering().
clear()`- clears all the elements of the main list given to the adapter & `add(c), adds the filtered list to the list.
I was getting Concurrent Modification exceptio at this line:
for (Item c : filteredList)
So I removed this line:
ArrayList<Item> filteredList = (ArrayList<Item>) results.values;
& added these lines
ArrayList<Item> filteredList = new ArrayList<Item>();
filteredList.addAll((Collection<? extends Item>) results.values);
But now I'm getting Null Pointer exception in the getview method. But according to my code, it should go to the getview, only if the filtered resuls has some values. then Why I'm getting this exception?
Thanks in Advance..