在Android中,从互联网获取数据时使用ProgressDialog.show()和ProgressDialog.hide()问题
当我定义进度对话框函数时,
public static void showLoadingBar(Context context)
{
dialog=new ProgressDialog(context);
dialog.setMessage("Please wait");
dialog.show();
}
public static void hideLoadingBar()
{
dialog.dismiss();
}
我想像下面这样使用它:
UiManager.getInstance().showLoadingBar(this);
FetchData();
UiManager.getInstance().hideLoadingBar();
但我永远无法显示 LoadingBar,直到我评论 UiManager.getInstance().hideLoadingBar();像这样的行
UiManager.getInstance().showLoadingBar(this);
FetchData();
//UiManager.getInstance().hideLoadingBar();
是什么原因,总是在屏幕上显示进度条。有什么办法可以解决这个问题吗?
When I define progress dialog functions such as
public static void showLoadingBar(Context context)
{
dialog=new ProgressDialog(context);
dialog.setMessage("Please wait");
dialog.show();
}
public static void hideLoadingBar()
{
dialog.dismiss();
}
I wanna use it like following:
UiManager.getInstance().showLoadingBar(this);
FetchData();
UiManager.getInstance().hideLoadingBar();
But I have never be able to show LoadingBar until I comment the
UiManager.getInstance().hideLoadingBar(); line such like that
UiManager.getInstance().showLoadingBar(this);
FetchData();
//UiManager.getInstance().hideLoadingBar();
What this cause is, always ProgressBar on the screen. Is there anyway to get rid of this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FetchData()
似乎是一个异步操作。因此,在实际操作完成之前,该函数返回并隐藏加载栏。我建议您使用 AsyncTask。要在 AsyncTask 运行时显示进度对话框,您可以在
onPreExecute()
中调用show()
并在中调用
hide()
onPostExecute()。从doInBackground()
调用FetchData()
。这将在 AsyncTask 执行其后台方法之前启动 ProgressDialog,并在完成时停止 ProgressDialog。FetchData()
seems to be an asynchronous operation. So, before the actual operation is complete, the function returns and hides the loading bar. I suggest you use an AsyncTask.To show a progress dialog while an AsyncTask runs, you may call
show()
inonPreExecute()
and callhide()
inonPostExecute()
. CallFetchData()
fromdoInBackground()
. This will start the ProgressDialog before the AsyncTask does it's background method and will stop the ProgressDialog when it completes.