ProgressDialog在单独的线程中 [英] ProgressDialog in a separate thread
问题描述
我有一个从数据库中提取数据并将其填充到列表中的过程.我想在执行查询时显示进度对话框,但仅在执行查询后才可视地显示.我相信我必须在单独的线程中运行ProgressDialog
,但遵循的建议很少,无法正常工作.
I have a procedure that extracts data from a database and populates it to the list. I want to display progress dialog box while query is executed, but it visually appears only after the query is executed. I believe I have to run a ProgressDialog
in a separate thread, but followed few suggestions and could not make it work.
所以在我的Activity
中我只有
private void DisplayAllproductListView(String SqlStatement) {
ProgressDialog dialog =
ProgressDialog.show(MyActivity.context, "Loading", "Please wait...", true);
//..................
//..................
//execute sql query here
dialog.dismiss();
}
谢谢
推荐答案
1.在主线程中显示进程对话框
1.show your process dialog in main thread
2.启动一个新线程(例如线程A)以处理繁重的工作
2.start a new thread (such as Thread A) to process your heavy job
3.完成后,使用处理程序从线程A向主线程发送消息,主线程关闭进程对话框
3.when done, use handler to send a message from Thread A to main thread, the latter dismisses the process dialog
这样的代码 私人ProcessDialog pd;
code like this private ProcessDialog pd;
private void startDialog()
{
pd = ProgressDialog.show(MainActivity.this, "title", "loading");
//start a new thread to process job
new Thread(new Runnable() {
@Override
public void run() {
//heavy job here
//send message to main thread
handler.sendEmptyMessage(0);
}
}).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
}
};
这篇关于ProgressDialog在单独的线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!