基本上,我有一个实现接口的Activity,并且有几个继承的函数。将调用这些函数中的每一个并传递将用于更新我的List适配器的数据。这是片段:
@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;
}
}
所以你可以看到,我当前正在更新我的List,这是一个可扩展的列表视图...... 但每次我收到此错误: Android,ListView IllegalStateException:“适配器的内容已更改但ListView未收到通知”!
我真的很困惑,我不知道在哪里正确放置这些程序以避免此错误仍然可以更新我的列表:(
请帮助!
答案 0 :(得分:1)
尝试在UI线程(mAdapterContact.notifyDataSetChanged();
)上运行Activity.RunOnUIThread
,并仔细检查mAdapterContact
是否为真正的列表适配器。
另外,请尝试在notifyDataSetChanged()
setAdapter()