【发布时间】:2010-08-07 15:38:13
【问题描述】:
我正在从我的 Android 应用程序的数据库中读取几千个项目,并希望显示一个加载对话框,启动一个线程并读取数据库,然后关闭对话框,然后用数据填充列表视图。 我似乎无法弄清楚这一点。
我弹出并消失了对话框,但我不知道在线程完成后如何填充列表视图。
有什么想法吗?
【问题讨论】:
标签: android multithreading listview
我正在从我的 Android 应用程序的数据库中读取几千个项目,并希望显示一个加载对话框,启动一个线程并读取数据库,然后关闭对话框,然后用数据填充列表视图。 我似乎无法弄清楚这一点。
我弹出并消失了对话框,但我不知道在线程完成后如何填充列表视图。
有什么想法吗?
【问题讨论】:
标签: android multithreading listview
使用 AsyncTask,在 onPostExecute() 中填充列表视图。
http://www.screaming-penguin.com/node/7746
private class InsertDataTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Main.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Inserting data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
//do something in background, i.e. loading data
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// populate list here
}
}
...
new InsertDataTask ().execute();
也很有帮助: http://developer.android.com/resources/articles/painless-threading.html
【讨论】: