ProgressDialog出现得太晚并且消失得太快
当我更新数据库时,我想显示一个进度对话框。我的问题是,ProgressDialog 出现得很晚,4-5 秒后,然后出现和消失得非常快,它在屏幕上停留几毫秒,几乎你看不到它,然后新数据立即显示在列表中。这让我认为 ProgressDialog 正在等待数据库更新(不需要太多时间,大约 4.5 秒),然后它显示在屏幕上,但很快就消失了。我希望按下“更新”按钮后立即出现 ProgressDialog,并在屏幕上停留大约 4-5 秒。
class MyAsyncTask extends AsyncTask<Void, Void, Void>{
ProgressDialog myprogsdial;
@Override
protected void onPreExecute(){
myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
RefreshDataBase();
}
});
return null;
}
@Override
protected void onPostExecute(Void result){
myprogsdial.dismiss();
}
}
当我调用它时,new MyAsyncTask().execute();
While I'm updating my database I want to display a progress dialog. My problem is that the ProgressDialog is getting late to appear,after 4-5 seconds, then appears and disappears very fast, it stays on screen few milliseconds almost you can't see it, then new data are shown in the list immediately. This makes me think that the ProgressDialog is waiting for database to be updated(it doesn't take much, about 4,5 seconds) and then it shows on the screen but is dismissing very fast. I would like the ProgressDialog appear immediately I press the 'Update' button and stay on the screen about 4-5 seconds.
class MyAsyncTask extends AsyncTask<Void, Void, Void>{
ProgressDialog myprogsdial;
@Override
protected void onPreExecute(){
myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
RefreshDataBase();
}
});
return null;
}
@Override
protected void onPostExecute(Void result){
myprogsdial.dismiss();
}
}
When I call it, new MyAsyncTask().execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我认为这个
导致了这种行为。doInBackground
() 在主 UI 线程的新线程中执行代码。然后,您将此线程中要执行的代码放回到主线程中,从而导致进度对话框最后延迟,然后在 postExecute() 中它立即关闭
。 rel="noreferrer">asyntask 教程可以在这里找到。
Ok I think that this
is causing this behavior.
doInBackground() executes your code in a new thread to the main UI thread. You are then putting the code to execute in this thread back into the main one causing the progress dialog to be delayed at the end and then in postExecute() it gets closed immediately.
A good asyntask tutorial can be found here.
您不得使用
runOnUiThread
。您基本上所做的是:RefreshDataBase
) 并阻止 UI。您应该直接调用
RefreshDataBase()
。如果这个方法涉及UI,你就必须重构它。You must not use
runOnUiThread
. What you're basically did is:RefreshDataBase
) and blocks the UI.You should call
RefreshDataBase()
directly. And if this method touches UI, you have to refactor it.我已经使用弗拉基米尔·伊万诺夫的这个答案解决了这个问题。
我已将功能与外观分开。
我将功能(下载新数据)保留在
doInBackground()
中,并在onPostExecute()
中更新了列表:获取新的适配器,称为setListAdaper()
和notifyDataSetChanged
。当然,我退出了
runOnUiThread()
。感谢大家的提示。I have solved it, using this answer of Vladimir Ivanov.
I have separated the functionality by the appearance.
I have kept the functionality(downloading new data) in
doInBackground()
and inonPostExecute()
I updated the list: get the new adapter,calledsetListAdaper()
andnotifyDataSetChanged
.Of course, I quit using
runOnUiThread()
. Thanks to all for hints.