Android 进度对话框直到函数完成运行才显示(AsyncTask)
单击按钮时,我将调用函数中的异步类,并且需要显示进度对话框,直到它运行显示列表函数。但它只有在函数运行完毕并立即关闭后才会显示。请帮助我我在这里做错了什么。
public class FilterAsyncTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dispProgress;
@Override
protected void onPreExecute()
{
dispProgress = ProgressDialog.show(Filter.this, "Please wait...",
"Loading...", true, true);
}
protected Void doInBackground(Void... params) {
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
MerchantsActivity.displayList();
dispProgress.cancel();
finish();
}
}
When a button is clicked I'm calling the async class in a function and I need to show progressDialog until it runs the displaylist function. But it shows up only after the function finished running and closes immediately. Please help me what am I doing wrong here.
public class FilterAsyncTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dispProgress;
@Override
protected void onPreExecute()
{
dispProgress = ProgressDialog.show(Filter.this, "Please wait...",
"Loading...", true, true);
}
protected Void doInBackground(Void... params) {
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
MerchantsActivity.displayList();
dispProgress.cancel();
finish();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 AsyncTask 将立即完成,因为你在 doInBackground() 中什么也没做!这就是你的长期运行的后台非 UI 代码应该去的地方......
Your AsyncTask will complete immediately because you do exactly nothing in doInBackground()! That's where your long-running background non-UI code is supposed to go...
我建议您不要使用
static ProgressDialog#show
方法。而是执行new ProgressDialog()
并相应地初始化它,最后调用show()
。我从未使用过静态方法,也不知道它是如何工作的,但我使用了其他选项。此外,静态方法似乎没有可用的文档。I would recommend you not to use the
static ProgressDialog#show
method. Rather donew ProgressDialog()
and initialize it accordingly and finally callshow()
. I have never used the static method and do not know how it works, but I have used the other option. Furthermore the static method seems to have no available documentation.