【发布时间】:2011-07-28 01:59:57
【问题描述】:
我有一个相当复杂的问题。我已经阅读了一些与我的问题类似的帖子,但情况略有不同。
我有一个带有列表视图的活动。 Activity 创建一个具有长时间运行进程的线程。流程结束后,它调用处理程序更新列表视图并播放声音。如果我在同一个 Activity 中,它工作正常,但是当我在进程运行时单击 HOME 按钮,然后按下快捷方式或应用程序图标以启动 Activity,然后当进程结束时,我听到声音但列表视图未更新。任何想法为什么会发生这种情况?有些人和我也遇到了同样的方向变化问题。
// thread created to send the reply.
Thread replyThread = new Thread() {
public void run() {
**Http POST here to upload an image which takes a while.
//Once done, send a msg to handler to play a sound and update listview
Message msg = replyHandler.sendEmptyMessage(0);
}
}
处理程序:
final Handler replyHandler = new Handler() {
public void handleMessage(Message msg) {
fillData();
**Code to play sound.
}
}
fillData() 方法
// refresh the listview.
private void fillData() {
// Get all of the rows from the database and create the item list
**I actually have an activity before this one but to simplify the problem I said I have 1 Activity. Bundle is from previous Activity.
String conversationId = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
conversationId = extras
.getString(TestDbAdapter.KEY_CONVERSATION_ID);
}
if (mDbHelper != null) {
Cursor notesCursor = mDbHelper
.fetchAllMessagesByConversationId(conversationId);
startManagingCursor(notesCursor);
String[] from = new String[] { TestDbAdapter.KEY_BODY,
TestDbAdapter.KEY_TIMESTAMP };
int[] to = new int[] { R.id.text1, R.id.text2 };
// Now create a simple cursor adapter and set it to display
MessagesCursorAdapter notes = new MessagesCursorAdapter(this,
R.layout.messages_row, notesCursor, from, to, mDbHelper);
setListAdapter(notes);
}
}
我知道有些人会说“改用 notifyDataSetChanged”,就这样做了。上面的代码以与 notifyDataSetChanged 相同的方式工作,但可能不是以一种非常优雅的方式,但在我的情况下它可以工作,但它对我使用 notifyDataSetChanged 时遇到的问题没有帮助。
【问题讨论】:
-
尝试使用新数据创建适配器/游标的新实例?
-
我已经实现了一个广播接收器来接收意图,或者一个回复处理程序,它可以工作。
标签: android multithreading listview