Android(安卓)学习之那些年我们遇到的BUG8:ArrayAdapter 直接使用 notifyDataSetChanged()无效
16lz
2021-01-26
BUG:在使用AutoCompleteTextView时,用ArrayAdapter作为适配器,刷新数据时使用notifyDataSetChanged()无效。
修改 ArrayList 然后调用 notifyDataSetChanged() 对于ArrayAdapter 没有产生影响,里面的数据并未发生改变,造成 notifyDataSetChanged() 无效
直接使用 ArrayAdapter 自带的clear(),add(), insert() and remove() 等函数可解决。
原因:
查看 ArrayAdapter 的源码发现,ArrayAdapter 在调用 notifyDataSetChanged() 时,并未将 ArrayList 数据的修改同步到 ArrayAdapter 内部。
@Overridepublic void notifyDataSetChanged() {super.notifyDataSetChanged();mNotifyOnChange = true;}
而 add() 等函数则先将 ArrayList 数据的修改同步到 ArrayAdapter 内部,再调用父类的notifyDataSetChanged()
public void add(@Nullable T object) {synchronized (mLock) { if (mOriginalValues != null) { mOriginalValues.add(object); } else { mObjects.add(object);}mObjectsFromResources = false;}if (mNotifyOnChange) notifyDataSetChanged();}
其中的 mNotifyOnChange 变量可通过调用 setNotifyOnChange(notifyOnChange) 方法设置为 false,则再使用 add() 等函数时若要产生变化效果则需要手动调用notifyDataSetChanged() 方法。
/*** Control whether methods that change the list ({@link #add}, {@link #addAll(Collection)},* {@link #addAll(Object[])}, {@link #insert}, {@link #remove}, {@link #clear},* {@link #sort(Comparator)}) automatically call {@link #notifyDataSetChanged}. If set to* false, caller must manually call notifyDataSetChanged() to have the changes* reflected in the attached view.** The default is true, and calling notifyDataSetChanged()* resets the flag to true.** @param notifyOnChange if true, modifications to the list will* automatically call {@link* #notifyDataSetChanged}*/public void setNotifyOnChange(boolean notifyOnChange) {mNotifyOnChange = notifyOnChange;}
第一次尝试分析 Android 源码!如有错误,烦请指正。
更多相关文章
- 一句话锁定MySQL数据占用元凶
- Service的onStartCommand方法的疑问
- Android(安卓)官方数据绑定 Data Binding Library
- Android(安卓)在低版本sdk中没有getSupportedPreviewFrameRates
- Android(安卓)- base - SQLite
- Android启动流程分析(七) init.rc的解析
- 意图对象传递数据
- OkHttpClient加载图片给DisposableObserver,CompositeDisposable
- Android(安卓)getView中的position问题解决方案