美文网首页
ListView问题: The content of the a

ListView问题: The content of the a

作者: 欠儿不登 | 来源:发表于2018-03-02 17:55 被阅读0次

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<T> extends BaseAdapter {

    protected ArrayList<T> mData;

    protected Context mContext;

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

    /**
     * <p>
     * 使用 ArrayList.clone()保证Adapter里的数据完全是由adapter维护。
     * </p>
     * 这样就可以先在子线程中加载数据,在UI线程中调用该方法刷新界面,保证UI的流畅性。
     */
    public void setData(ArrayList<T> dataList) {
        if (dataList != null) {
            this.mData = (ArrayList<T>) dataList.clone();
            notifyDataSetChanged();
        }
    }

    public ArrayList<T> 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 a

      本文链接:https://www.haomeiwen.com/subject/hxaaxftx.html