ListView控件IllegalStateException异常:“适配器的内容发生了变化,但ListView控件没有收到通知” [英] ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”

查看:139
本文介绍了ListView控件IllegalStateException异常:“适配器的内容发生了变化,但ListView控件没有收到通知”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一点我们已经在这里讨论<一href="http://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed">Android, ListView控件IllegalStateException异常:&QUOT;适配器的内容发生了变化,但ListView控件没有收到通知&QUOT; 但我仍然有问题。 我已经得到了服务,其中dowloads数据网络,将其保存做数据库,并触发目的时,它的完成。我的活动有ListView和广播接收器:

 私有类NewsUpdateReceiver扩展的BroadcastReceiver {

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){

            mNotificationManager.cancel(NewsService.NOTIFICATION_ID);

            loadNewsFromDB();
        }
    }

 私人无效loadNewsFromDB(){
        mList.clear();
        ContentResolver的CR = getContentResolver();

        光标光标= cr.query(MeidahonProvider.CONTENT_URI,NULL,NULL,NULL,NULL);

        如果(cursor.moveToFirst()){
            做{
                字符串标题= cursor.getString(MeidahonProvider.TITLE_COLUMN);
                字符串描述= cursor.getString(MeidahonProvider.DESCRIPTION_COLUMN);
                字符串link = cursor.getString(MeidahonProvider.LINK_COLUMN);
                长datems = cursor.getLong(MeidahonProvider.DATE_COLUMN);
                日期日期=新的日期(datems);
                NewsItem项目=新NewsItem(标题,描述,链接,日期);
                mList.add(项目);
                mAdapter.notifyDataSetChanged();
            }而(cursor.moveToNext());
        }
        cursor.close();
    }
 

当项目被添加到ArrayList中,我打电话每次 mAdapter.notifyDataSetChanged(); ,但仍然有例外。我该如何解决这个问题呢?

UPD LOG

  java.lang.IllegalStateException:适配器的内容发生了变化,但ListView控件没有收到通知。确保您的适配器的内容是不是从后台线程修改,但只能从UI线程。 [中的ListView(2131296334,类android.widget.ListView)与适配器(类com.transportoid.Tracks.TrackListAdapter)
在android.widget.ListView.layoutChildren(ListView.java:1432)
在android.widget.AbsListView.onTouchEvent(AbsListView.java:2062)
在android.widget.ListView.onTouchEvent(ListView.java:3234)
在android.view.View.dispatchTouchEvent(View.java:3709)
在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 

解决方案

首先,除非你需要为您的各种有界组件被告知在每次迭代中,我只会叫转接适配器含量的变化。 notifyDataSetChanged()你已经完成了所有添加后。

其次,可以提供异常和堆栈跟踪?

三,什么线程正在与调用这个code(主界面分派线程或背景)?


感谢Migher的更新logcat的。帮我一个忙,帮助澄清一个观点:可以在运行一个快速测试,看看会发生什么,当你手动输入相同的数据值...从菜单中选择或一个按钮单击事件做(这样我们就知道它是在GUI线程)?得到同样的错误?

另外值得指出的是给SimpleCursorAdapter一试,而不是您当前使用的常规适配器。正如我无法通过我的调试器中运行这个,这可能只是比去来回异步技术支持的建议解决方法更为简单,因为我相信你会喜欢这个固定宜早不宜迟。

让我知道无论这些工作 - 谢谢

This was discussed earlier here Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification" but I still have the problem. I've got Service, which dowloads data from web, saves it do db and fires intent when it's completed. My Activity has ListView and BroadCast receiver:

private class NewsUpdateReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            mNotificationManager.cancel(NewsService.NOTIFICATION_ID);

            loadNewsFromDB();
        }
    }

 private void loadNewsFromDB(){
        mList.clear();
        ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(MeidahonProvider.CONTENT_URI, null, null, null, null);

        if(cursor.moveToFirst()){
            do{
                String title = cursor.getString(MeidahonProvider.TITLE_COLUMN);
                String description = cursor.getString(MeidahonProvider.DESCRIPTION_COLUMN);
                String link = cursor.getString(MeidahonProvider.LINK_COLUMN);
                long datems = cursor.getLong(MeidahonProvider.DATE_COLUMN);
                Date date=new Date(datems);
                NewsItem item = new NewsItem(title, description, link, date);
                mList.add(item);
                mAdapter.notifyDataSetChanged();
            }while(cursor.moveToNext());
        }
        cursor.close();
    }

Every time when item is add to ArrayList, I call mAdapter.notifyDataSetChanged(); but still have exception. How can I solve the problem?

UPD LOG

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. [in ListView(2131296334, class android.widget.ListView) with Adapter(class com.transportoid.Tracks.TrackListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1432)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2062)
at android.widget.ListView.onTouchEvent(ListView.java:3234)
at android.view.View.dispatchTouchEvent(View.java:3709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

解决方案

First off, unless you "need" for your various bounded components to be made aware of the change in the adapter content on every iteration I would only call adapter.notifyDataSetChanged() after you've completed all the additions.

Secondly, can you provide the Exception and StackTrace?

Third, what Thread is this code being invoked with (the main GUI dispatching thread or a background)?


Thanks Migher for the updated Logcat. Do me a favor to help clarify a point: can you run a quick test to see what happens when you manually enter the same data values ... do it from a menu selection or a button click event (so we'll know it's on the GUI thread)? Getting the same error?

Also worth pointing out is give a SimpleCursorAdapter a try instead of the regular Adapter you're currently using. As I'm not able to run this through my debugger this might be just a simpler fix than going back and forth with "asynchronous tech support" suggestions, as I'm sure you'd like this fixed sooner rather than later.

Let me know how either of these work - thanks

这篇关于ListView控件IllegalStateException异常:“适配器的内容发生了变化,但ListView控件没有收到通知”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆