Android开发教程--关于Adapter的The content of the adapter has changed问题分析

我们在做android list开发的时候,有时候会遇到类似这样的问题:

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.

意思大体是,你的adapter的内容变化了,但是你的ListView并不知情。请保证你adapter的数据在主线程中进行更改!

通过查询Android源码看看该Exception是从哪里抛出来的。

在ListView的layoutChildren()方法里有如下一段方法:

// Handle the empty set by removing all views that are visible

// and calling it a day

if (mItemCount == 0) {

resetList();

invokeOnItemScrollListener();

return;

} 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. [in ListView(" + getId() + ", " + getClass()

+ ") with Adapter(" + mAdapter.getClass() + ")]");

}

意思是:当ListView缓存的数据Count和ListView中Adapter.getCount()不等时,会抛出该异常。

知道了原因,改起来就好办多了。

就是在你进行adapter内item进行增加、删除的时候,一定要同步进行notifyDataSetChanged()方法通知更新。并且同时在主线程中进行。

这样这个问题就解决了!

时间: 2024-08-03 06:27:54

Android开发教程--关于Adapter的The content of the adapter has changed问题分析的相关文章

[Android 开发教程(1)]-- Saving Data in SQL Databases

Saving data to a database is ideal for repeating or structured data, such as contact information. This class assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android. The APIs you'll need

ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置

原文地址: ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/arcgis_mobile/article/details/8113948   2.开发环境配置 2.1 系统要求 1)      支持的操作系统 A.      Windows XP(32位).Vista(32/64位).Windows 7(32/64位) B.      Mac

ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map

原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/arcgis_mobile/article/details/8124005 通过上面章节,我们已经了解如何配置一个ArcGIS Runtime for Android开发环境,下面我们将介绍如何使用Eclipse创建一个ArcGIS移动项目Hello W

ArcGIS Runtime for Android开发教程V2.0(1)基本概念

原文地址: ArcGIS Runtime for Android开发教程V2.0(1)基本概念 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/arcgis_mobile/article/details/8113183   1.基本概念 1.1 Android简介         2007年11月,Google推出移动操作系统Android,并宣称Android是首个为移动终端打造的真正开放和完整的移动操作系统.自此,基于And

android开发教程:android调用activity简单方法

在没看麦子学院的教学视频之前,觉得在一个apk中调用另外一个apk中的activity是件非常麻烦的事情,有时候很害怕遇见这类问题,但是看了android开发教程视频之后,觉得这本来是一件很简单的事情,所以写篇小文章供参考. 系统提供了很多可以直接调用的Activity,通过指定的Intent就可以调用,比如打开搜索的: Java代码 1. Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 2. intent.putExtra(Searc

android开发教程:android手势翻页效果

听麦子学院android开发老师说要实现手势翻页效果,主要用到ViewFlipper和GestureDetector.  ViewFlipper变化当前显示内容,GestureDetector监听手势.  用于多页的展示非常酷.  现在我就给大家简单说明下,  首先创建工程:TestFlip,创建主Activity:TestFlip.  在res/layout/main.xml中添加flipper信息,如下:  Java代码   1. <?xml version="1.0" en

Android开发教程复选框详解

前面麦子学院的android开发老师给大家介绍过关于Android开发教程单选框详解,今天麦子学院的android开发老师给大家讲android复选框的一些基本内容. ●设置复选框的Check状态的时候,调用setChecked()方法 ●追加Android复选框被选择时处理的时候, 1.调用setOnCheckedChangeListener()方法,并把CompoundButton.OnCheckedChangeListener实例作为参数传入 2.在CompoundButton.OnChe

ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView

原文地址: ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/arcgis_mobile/article/details/8147328 MapView是ArcGIS Runtime SDK forAndroid的核心组件,通过MapView可以呈现地图服务的数据,并且在MapView中定义了丰富的属性.方法和事件,用户通过Map

ArcGIS Runtime for Android开发教程V2.0(8)基础篇-----地图事件

转自:http://blog.csdn.net/arcgis_mobile/article/details/8263283 ArcGIS Runtime sdk for Android为我们提供了丰富的事件监听器,本节将主要介绍我们经常使用的监听器,并且介绍通过这些监听器可以实现哪些功能,在下面的监听器中只有MapOnTouchListener是类,其他皆为接口类型,如图所示: 1.1 MapOnTouchListener MapOnTouchListener是MapView最为重要的监听器之一

Unity3D For Android 开发教程

原地址:http://game.ceeger.com/Unity/Doc/2011/Unity3D_For_Android.html 我自认为抵挡诱惑的能力还是很强大的,关键的时候还能把持住自己.今天逛了一下南京的丹凤街,终于受不住Android这美眉的诱惑.她虽脸蛋不怎么滴,但身材火热,且性感,廉价,低碳!长相平凡,却气质绝佳!虽出道不久,却已经在江湖搅起一股血雨腥风.在这阳光明媚的一天我给大家讲讲Unity3D的小安开发步骤.其实就是发布步骤啦! 第一步: 先去下载一个Android-SDK