ListView问题: The content of the adapter has changed but ListView did not receive a notification.

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(xxxxx, class android.widget.ListView) with Adapter

第一种解决思路:http://www.cnblogs.com/monodin/p/3874147.html

第二种解决思路:http://blog.csdn.net/ueryueryuery/article/details/20607845

因为需要加载数据较多,构建数据需要较长时间,如果放在主线获取数据源,会导致UI卡顿
遂采用第二种方案,获取数据放在子线程,更新数据时采用clone的方式保证Adapter的数据源完全由Adapter自身维护

public abstract class EventAdapter extends BaseAdapter {

    protected ArrayList mData;

    protected Context mContext;

    public EventAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 

* 使用 ArrayList.clone()保证Adapter里的数据完全是由adapter维护。 *

* 这样就可以先在子线程中加载数据,在UI线程中调用该方法刷新界面,保证UI的流畅性。 */ public void setData(ArrayList dataList) { if (dataList != null) { this.mData = (ArrayList) dataList.clone(); notifyDataSetChanged(); } } public ArrayList getData() { return mData; } @Override public int getCount() { return mData != null ? mData.size() : 0; } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } }

你可能感兴趣的:(ListView问题: The content of the adapter has changed but ListView did not receive a notification.)