I have my own ArrayAdapter
and inside of this adapter class I have a Filter
. In this filter I am overriding the publishResults(CharSequence constraint, FilterResults result)
method.
@Override
protected void publishResults(CharSequence constraint, FilterResults result) {
final ArrayList<Car> filtered = (ArrayList<Car>) results.values;
for (int i = 0, l = filtered.size(); i < l; i++) {
add(filtered.get(i));
}
}
The method above works fine, when I only have 100 or so entries. When my entries get into the thousands, the UI Thread hangs for a few seconds. I thought I would just move this to an AsyncTask
, but it crashes. What would be the best way to handle my results without having the UI hang?
Code that crashes (seems to start, and crash in the middle of the for loop:
@Override
protected void publishResults(CharSequence constraint, FilterResults result) {
final ArrayList<Car> filtered = (ArrayList<Car>) results.values;
new LongOperation().execute(filtered);
}
private class LongOperation extends AsyncTask<ArrayList<Car>, Void, Void> {
@Override
protected Void doInBackground(ArrayList<Car>... params) {
for (int i = 0, l = params[0].size(); i < l; i++) {
add(params[0].get(i));
}
return null;
}
@Override
protected void onPostExecute(Void result) {
notifyDataSetChanged();
}
}
};
Here is the crash message. I am using commonsware MergeAdapter to combine two adapters, so maybe that may be the issue?
E/AndroidRuntime(22868): java.lang.IllegalStateException:
The content of the adapter has changed but ListView did not receive a notification.
Make sure the content of your adapter is not modified from a background thread,
but only from the UI thread. Make sure your adapter calls notifyDataSetChanged()
when its content changes. [in ListView(16908298, class android.widget.ListView)
with Adapter(class com.commonsware.cwac.merge.MergeAdapter)]