【问题标题】:Android, ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”!Android,ListView IllegalStateException:“适配器的内容已更改,但ListView没有收到通知”!
【发布时间】:2012-03-16 01:58:49
【问题描述】:

所以基本上,我有一个实现接口并具有几个继承功能的 Activity。这些函数中的每一个都将被调用并传递将用于更新我的列表适配器的数据。这是sn-p:

@Override
public void onEntriesAdded(List<String> addresses) throws RemoteException {
    Log.v(TAG, "Entries Added notify!");
    listEntriesAdd.clear();
    listEntriesAdd = addresses;
    rosterHandler.sendEmptyMessage(1);
}

@Override
public void onEntriesUpdated(List<String> addresses) throws RemoteException {
    Log.v(TAG, "Entries Updated notify!");
    listEntriesUpdate.clear();
    listEntriesUpdate = addresses;
    rosterHandler.sendEmptyMessage(2);
}

@Override
public void onEntriesDeleted(List<String> addresses) throws RemoteException {
    Log.v(TAG, "onEntries deleted " + addresses);
    listEntriesDel.clear();
    listEntriesDel = addresses;
    rosterHandler.sendEmptyMessage(3);
}

因为我以前见过任何解决方案,所以我将每个函数的工作传递给一个 Handler:

private Handler rosterHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        case 0:
            // adding or any List<String> elements operations
            break;

        case 1:
            // adding or any List<String> elements operations
            break;

        case 2:
            // adding or any List<String> elements operations
            break;

        case 3:
            // adding or any List<String> elements operations
            break;

        case 4:
            // adding or any List<String> elements operations
            break;
        }
        mAdapterContact.notifyDataSetChanged();
    }
};

然后,当我执行 bindService 时会调用一个 ServiceConnection 类:

 @Override
protected void onResume() {
super.onResume();
    if (!mBinded) {
        Log.d(TAG, "Request BIND Service");
        mBinded = bindService(SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
    }
}

和服务类:

 private class MyServiceConnection implements ServiceConnection {

public MyServiceConnection() {
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
    globalBinder = service;
    mXmppFacade = IXmppFacade.Stub.asInterface(service);
    try {
    mRoster = mXmppFacade.getRoster();
    if (mRoster != null) {
        Log.i(TAG, "REBUILD ROSTER!!!");
        rosterHandler.sendEmptyMessage(0);// send it to handler
    }
    } catch (RemoteException e) {
    e.printStackTrace();
    }
}

@Override
public void onServiceDisconnected(ComponentName name) {
    try {
    mRoster.removeRosterListener(ContactList.this);
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    mXmppFacade = null;
    mChatManager = null;
    mRoster = null;
    mListContact.clear();
    mListGroup.clear();
    mContactOnGroup.clear();
    mBinded = false;
}
}

如您所见,我实际上正在更新我的列表,一个像这样的可扩展列表视图...... 但每次我得到这个错误:Android,ListView IllegalStateException:“适配器的内容已经改变,但ListView没有收到通知”!

我真的很困惑,我不知道在哪里正确放置这些程序以避免这个错误并且仍然可以更新我的列表:(

请帮忙!

【问题讨论】:

  • 当你的适配器的内容已经改变并且适配器需要更新视图,但是你没有使用notifyDataChanged,它会抛出这个错误。从上面的代码,我不知道你的适配器的数据是哪个列表。

标签: android listview service xmpp handler


【解决方案1】:

尝试在 UI 线程 (Activity.RunOnUIThread) 上运行 mAdapterContact.notifyDataSetChanged();,并仔细检查 mAdapterContact 是真正的列表适配器。 另外,请尝试在setAdapter() 之后立即致电notifyDataSetChanged()

【讨论】:

  • 你的意思是,我需要在我的活动中添加更多线程吗? 'n 实际上,我只在 onCreate 时调用 setAdapter...但谢谢,我会试试的
  • 我的意思是,该服务可以在其他线程中发送消息,这可能是个问题。
  • 我的眼睛不好抱歉:p list.setAdapter(new ContactGroupAdapter(this, mGroup, mChild));
猜你喜欢
  • 2011-03-09
  • 2011-09-22
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多