在android中使用progressDialog?
我正在使用此代码来显示一个工作正常的进度对话框:
dialog = ProgressDialog.show(this, "Please wait",
"Gathering Information...", true);
Thread thread = new Thread()
{
@Override
public void run() {
if(Chapter_sync.size()>0){
storemodule();
c.open();
for(int i=0;i<Chapter_sync.size();i++)
{
downloadPDF(Chapter_sync.get(i));
System.out.println("SYNCED"+i);
c.update(Chapter_sync.get(i));
}
}dialog.dismiss();
}
};thread.start();
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
parentlayout.removeAllViews();
setUpViews();
}
}
这里我想做的是显示一个进度对话框,直到所有计算完成。 当它完成时,我想再次设置所有视图。但是 setUpViews() 在线程启动之前被调用。我不太擅长线程基础知识。有人可以帮助我理解为什么会发生这种情况以及我怎样才能得到自己的结果吗?
I am using this code to display a Progress Dialog which is working fine:
dialog = ProgressDialog.show(this, "Please wait",
"Gathering Information...", true);
Thread thread = new Thread()
{
@Override
public void run() {
if(Chapter_sync.size()>0){
storemodule();
c.open();
for(int i=0;i<Chapter_sync.size();i++)
{
downloadPDF(Chapter_sync.get(i));
System.out.println("SYNCED"+i);
c.update(Chapter_sync.get(i));
}
}dialog.dismiss();
}
};thread.start();
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
parentlayout.removeAllViews();
setUpViews();
}
}
Here what I am trying to do is display a Progress dialog till all computation is done.
As it completes i wanted to setup all views again. But the setUpViews() is called before the thread starts. I am not so good at thread basics .Could any one help me understand why is this happening and how can I get my own results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您没有使用处理程序。只需执行此操作,
并在 onCreate() 创建处理程序中,
您就无法从后台线程更新 UI。您必须使用 AsyncTask 或使用后台线程中的处理程序来通知主线程后台操作已完成。
The problem is you are not using handlers. Simply do this,
And in your onCreate() create Handlers,
You can't update your UI from background thread. Either you have to use AsyncTask or to use handlers from your background thread to inform your main thread that the background action has been completed.
线程调度依赖于操作系统。因此实例化你的线程并不能确保你的线程会在你需要的时候运行。
使用异步任务可以最好地解决您面临的问题。或者,如果您有一个回调让您知道下载何时完成,那么您可以关闭回调上的对话框。确保通过执行以下操作在 UI 线程内将其关闭。
mActivity.runOnUiThread() 或任何其他此类方法。
Thread scheduling is dependent on the operating system. So instantiating your thread does not ensure that your thread will run whenever you want.
The problem you are facing can be best handled using async task. Or if you have a callback that lets you know when your download is completed then you can dismiss the dialog on the callback. Make sure you dismiss it inside a UI thread by doing.
mActivity.runOnUiThread() or any other such methods.
在您的代码中,如果您看到
启动线程后,您已调用方法 setUpViews(),该方法不会等待您的线程完成并设置您的视图。
在线程中关闭对话框后使用 Handler.post 来收集您的信息。
因此,在您的操作完成后,您的处理程序将调用您的 setupViews。
In your code if u see
After Starting the Thread you have call your method setUpViews(), which does not wait for your thread to complete and setups your views.
Use Handler.post after the dialog is dismissed in your thread which gather your information.
So after the your operations completed your setupViews will be called by your Handler.