我在修改列表视图时收到此异常。
适配器的内容已更改,但ListView未收到通知。内容适配器不会从后台线程修改。
但是我没有在后台线程上修改任何内容,而且我也没有经常这样做。只有在我有多个pns的情况下,如果设备进入睡眠模式,之后解锁时,应用程序点击列表视图会导致崩溃。
以下是我为在ListView
中显示消息而编写的代码。
private class GetMessagesAsyncronousTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog myAsyncTask = null;
@Override
protected void onPreExecute() {
myAsyncTask = new ProgressDialog(MessageActivity.this);
myAsyncTask.setMessage(getString(R.string.msg_refresh_label));
myAsyncTask.setIndeterminate(true);
myAsyncTask.setCancelable(false);
if (myAsyncTask != null && !myAsyncTask.isShowing()) {
try {
myAsyncTask.show();
} catch (WindowManager.BadTokenException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Here i am calling the web services and inserting the data in DB.
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (myAsyncTask != null && myAsyncTask.isShowing()) {
try {
myAsyncTask.dismiss();
myAsyncTask = null;
} catch (WindowManager.BadTokenException e1) {
e1.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
}
}
// this method is for updating the messages Count in spinner and list view rows when push notification came.
resetAdapter();
}
}
}
在ResetAdapter
中,基于微调器选择,我在listview
中显示相应的消息。
DropdownAdapter dropDownAdapter = new DropdownAdapter(
MessageActivity.this, R.layout.row_dropdown_action,
listNavigationArray);
dropDownAdapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition,
long itemId) {
navigateToFolder(itemPosition);
return false;
}
};
getActionBar().setListNavigationCallbacks(dropDownAdapter,
navigationListener);
}
//导航文件夹方法。
private void navigateToFolder(int itemPosition) {
if (itemPosition == 2) {
listView.setVisibility(View.GONE);
archiveListView.setVisibility(View.VISIBLE);
recipientListView.setVisibility(View.GONE);
final List<MessageBean> readMessageList = messageManager
.getMessageList("read", user.getSponsorId(), user.getUserId());
final CheckBoxAdapter checkBoxAdapter = new CheckBoxAdapter(
MessageActivity.this, readMessageList);
archiveListView.setAdapter(checkBoxAdapter );
} else if (itemPosition == 3) {
recipientListView.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
archiveListView.setVisibility(View.GONE);
adapter = new AddressAdapter(MessageActivity.this,
R.layout.row_address, matchedRecipients);
recipientListView.setAdapter(adapter);
} else if (itemPosition == 0) {
listView.setVisibility(View.VISIBLE);
archiveListView.setVisibility(View.GONE);
recipientListView.setVisibility(View.GONE);
messageList = messageManager.getUnreadMessageList("unread",
user.getSponsorId(), user.getUserId());
inboxAdapter = new MessageListAdapter(MessageActivity.this,
R.layout.custome_row_message, messageList);
refreshYourAdapter(messageList);
listView.setAdapter(inboxAdapter);
} else if (itemPosition == 1) {
listView.setVisibility(View.VISIBLE);
archiveListView.setVisibility(View.GONE);
recipientListView.setVisibility(View.GONE);
inboxAdapter = new MessageListAdapter(MessageActivity.this,
R.layout.custome_row_message, pendingMessageList);
refreshYourAdapter(pendingMessageList);
listView.setAdapter(inboxAdapter);
}
}
private void refreshYourAdapter(final List<MessageBean> items) {
runOnUiThread(new Runnable() {
public void run() {
if (inboxAdapter != null) {
inboxAdapter.refreshAdapter(items);
}
}
});
}