Override ListView getAdapter造成的后果

Override ListView getAdapter造成的后果

近期工作中,发现了一个bug,是和ListView Adapter有关的。产生了FC,描写叙述信息大约是

"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(xxx) with Adapter(HeaderViewListAdapter)]"

它的大意是,Adapter内的数据发生了变化,可是UI却没有更新,您是否忘记调用了notifyDataSetChanged?

这实际上是一个很有误导的信息。普通情况下,我们不会忘记调用该函数的。可是假设我们不小心,从listview继承一个新的类,并override它的getAdapter方法,就可能会出问题了。

ListView是支持HeaderView和footerView的,即在listview的最初和最末尾的位置加入�一些特殊的view。它的实现方法,就是通过一个HeaderViewListAdapter。

HeaderViewListAdapter会包装一个Adapter,这个是由用户自己设置的。ListView中相应的代码是

    @Override
    public void setAdapter(ListAdapter adapter) {
        if (mAdapter != null && mDataSetObserver != null) {
            mAdapter.unregisterDataSetObserver(mDataSetObserver);
        }

        resetList();
        mRecycler.clear();

        if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
            mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
        } else {
            mAdapter = adapter;
        }

ListView的getAdapter返回的是mAdapter,就可以能是一个HeaderViewListAdapter.

假设override getAdapter,并返回HeaderViewListAdapter内部包装的Adapter,就会出问题。也就是上面提到的FC.

这样的问题是怎么出现呢?

首先,这个异常抛出的位置,是在函数layoutChildren中,抛出的条件是mItemCount != mAdapter.getCount(),代码例如以下:

else if (mItemCount != mAdapter.getCount()) {
                throw new 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(" + getId() + ", " + getClass()
                        + ") with Adapter(" + mAdapter.getClass() + ")]");
            }

那么mItemCount的值是在哪里赋值呢?mItemCount不是ListView的成员,而是ListView的超超类:AdapterView的成员,这个值也是在DataObserver.onChanged中设置的,您可參考AdapterView的源代码:

class AdapterDataSetObserver extends DataSetObserver {

        private Parcelable mInstanceState = null;

        @Override
        public void onChanged() {
            mDataChanged = true;
            mOldItemCount = mItemCount;
            mItemCount = getAdapter().getCount(); //这里!注意使用方法getAdapter()

            // Detect the case where a cursor that was previously invalidated has
            // been repopulated with new data.
            if (AdapterView.this.getAdapter().hasStableIds() && mInstanceState != null
                    && mOldItemCount == 0 && mItemCount > 0) {
                AdapterView.this.onRestoreInstanceState(mInstanceState);
                mInstanceState = null;
            } else {
                rememberSyncState();
            }
            checkFocus();
            requestLayout();
        }

假设 getAdapter() != mAdapter就会发生故障:getAdatper返回的是mAdapter(即HeaderListViewAdapter),那么,mAdapter.getCount() == getAdapter().getCount() + header view count + footer view count.

出现上面的问题就在所难免了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119023.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker端口映射后访问不了_docker暴露多个端口

    docker端口映射后访问不了_docker暴露多个端口docker端口映射突然无效1、查看防火墙状态(systemctlstatusfirewalld),防火墙是关闭的[root@VM-0-15-centos~]#systemctlstatusfirewalld●firewalld.service-firewalld-dynamicfirewalldaemonLoaded:loaded(/usr/lib/systemd/system/firewalld.service;disabled;vendorp

    2022年10月18日
    1
  • 关于PHP程序员技术职业生涯规划

    关于PHP程序员技术职业生涯规划

    2021年10月15日
    39
  • C#中HttpWebRequest的用法详解

    C#中HttpWebRequest的用法详解HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择。它们支持一系列有用的属性。这两个类位于System.Net命名空间,默认情况下这个类对于控制台程序来说是可访问的。请注意,HttpWebRequest对象不是利用new关键字通过构造函数来创建的,而是利用工厂机制(factorymechanism)通过Create()方法来创建的。另外,你可…

    2022年7月15日
    15
  • split 方法_subsequence和substring的区别

    split 方法_subsequence和substring的区别1,split方法split()方法用于把一个字符串分割成字符串数组。例如:数据中现在有这样一个结果128b8f730592cc8db33ea52985127d44,44bee6555822d8321d2d1a2c1ac3b2cf,b2f939f26e512934e165f3e784cc74ca,我需要把这个字符串变成数组 console.log(res.result.produc…

    2022年9月25日
    0
  • pytest 执行用例_测试用例执行结果有哪些

    pytest 执行用例_测试用例执行结果有哪些前言平常我们功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间

    2022年7月29日
    5
  • SQL分页查询方案的性能对比[通俗易懂]

    SQL分页查询方案的性能对比[通俗易懂]作者|中国农业银行吴海存责编|晋兆雨头图|CSDN下载自视觉中国导读本文主要介绍了基于ROWNUM、主键列/非空唯一性列、分析函数、OFFSET-FETCHNEXT机制的…

    2022年6月26日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号