我正在使用Android中的adapters
,在极少数情况下,应用程序在为ListView
设置数据时崩溃...请帮帮忙。
例外情况如下
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.
[in ListView(2131558745, class android.widget.ListView)
with Adapter(class com.adapters.CustomToBeSyncAdapter)
这是我的代码段
private class ImportAsyncTask extends AsyncTask<Void, Integer, Integer> {
@Override
protected Integer doInBackground(Void... params) {
Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
try {
RealmResults<AssetsActivitiesRealm> mResultsActivities = realm.where(AssetsActivitiesRealm.class).findAll();
for (int i = 0; i < mResultsActivities.size(); i++) {
AssetsActivitiesRealm mAssetsActivitiePj = mResultsActivities.get(i);
SyncPojoList.add(new SyncPojo(mAssetsActivitiePj.getBatch(), mAssetsActivitiePj.getItemCode()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
realm.close();
return 1;
}
@Override
protected void onPreExecute() {
showLoading();
}
@Override
protected void onPostExecute(Integer sum) {
try {
cancelLoading();
displayListView();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void notifyAdapter() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
listViewSync.setAdapter(null);
if (mdataAdapter != null) {
mdataAdapter.notifyDataSetChanged();
listViewSync.requestLayout();
}
}
});
}
private void displayListView() {
mdataAdapter = new CustomToBeSyncAdapter(getActivity(),
R.layout.custom_tobesync_row, SyncPojoList);
notifyAdapter();
listViewSync.setAdapter(mdataAdapter);
listViewSync.invalidate();
}
答案 0 :(得分:1)
当您在AsyncTask#doInBackground()
内添加对象时,您仍在从后台线程更改ListView
适配器的模型。
尝试使用AsyncTask#publishProgress(Progress...)
中的for-loop
,然后覆盖AsyncTask#onProgressUpdate(Progress...)
,其中Progress
是您的模特。
所以你的for-loop
看起来像这样:
RealmResults<AssetsActivitiesRealm> mResultsActivities = realm.where(AssetsActivitiesRealm.class).findAll();
for (int i = 0; i < mResultsActivities.size(); i++) {
AssetsActivitiesRealm mAssetsActivitiePj = mResultsActivities.get(i);
publishProgress(new SyncPojo(mAssetsActivitiePj.getBatch(), mAssetsActivitiePj.getItemCode());
}
您的onProgressUpdate
可能如下所示:
public void onProgressUpdate(SyncPojo progress) {
SyncPojoList.add(progress);
mdataAdapter.notifyDataSetChanged(); // Notifying changes to the adapter here.
}
目前尚不清楚为什么你需要在AsyncTask
内运行所有这些,而且Realm实际上宣称你并不需要在后台线程中运行查询,因为它们的查询速度非常快(它们是在获取数据时使用延迟加载)在大多数情况下它应该影响UI线程 - 特别是当你根本没有任何“特殊”查询时。
另一方面,您无需创建Realm.Transaction
来查询数据。事务仅用于确保更改数据时的正确性。
你绝对可以简化你的代码:
requestLayout
后ListView
使用了notifyDatasetChanged
,这可能是无用的。try-catch
一般Exception
是出于什么原因?您应该先处理代码,然后才能导致异常,而不是仅在容易出错的代码周围添加try-catch
。AsyncTask
会返回一个无意义的整数,而不是实际返回结果。ListView
更新,因此无需致电notifyAdapter
。notifyAdapter
方法做了很多事情,它不需要做 - 例如将适配器设置为null
。希望这会有所帮助,否则我会很乐意详细说明任何方面。