【发布时间】:2014-06-27 17:47:42
【问题描述】:
我有自己的ArrayAdapter,在这个适配器类里面我有一个Filter。在这个过滤器中,我覆盖了publishResults(CharSequence constraint, FilterResults result) 方法。
@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));
}
}
当我只有 100 个左右的条目时,上面的方法可以正常工作。当我的条目达到数千时,UI 线程会挂起几秒钟。我以为我会把它移到AsyncTask,但它崩溃了。在不挂起 UI 的情况下处理我的结果的最佳方法是什么?
崩溃的代码(似乎开始,并在 for 循环中间崩溃:
@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();
}
}
};
这是崩溃消息。我正在使用 commonsware MergeAdapter 来组合两个适配器,所以这可能是问题所在?
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)]
【问题讨论】:
-
您似乎正在从
doInBackground()调用add()方法。这是在做什么? -
add() iscomingFrom ArrayAdapter - "在数组末尾添加指定的对象。"
标签: android android-asynctask android-arrayadapter android-adapter commonsware-cwac