在android中使用progressDialog?

发布于 2025-01-02 15:32:20 字数 904 浏览 1 评论 0原文

我正在使用此代码来显示一个工作正常的进度对话框:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱自由 2025-01-09 15:32:20

问题是您没有使用处理程序。只需执行此操作,

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();                           
          }
         handler.sendemptyMessage(0);
       };thread.start();

并在 onCreate() 创建处理程序中,

Handler handler=null;
handler=new Handler()
{
 public void handleMessage(Message msg)
{
 progressDialog.cancel();
  if(msg.what==0)
{
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
         parentlayout.removeAllViews();

         setUpViews();
};

您就无法从后台线程更新 UI。您必须使用 AsyncTask 或使用后台线程中的处理程序来通知主线程后台操作已完成。

The problem is you are not using handlers. Simply do this,

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();                           
          }
         handler.sendemptyMessage(0);
       };thread.start();

And in your onCreate() create Handlers,

Handler handler=null;
handler=new Handler()
{
 public void handleMessage(Message msg)
{
 progressDialog.cancel();
  if(msg.what==0)
{
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
         parentlayout.removeAllViews();

         setUpViews();
};

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.

空心空情空意 2025-01-09 15:32:20

线程调度依赖于操作系统。因此实例化你的线程并不能确保你的线程会在你需要的时候运行。

使用异步任务可以最好地解决您面临的问题。或者,如果您有一个回调让您知道下载何时完成,那么您可以关闭回调上的对话框。确保通过执行以下操作在 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.

各自安好 2025-01-09 15:32:20

在您的代码中,如果您看到

启动线程后,您已调用方法 setUpViews(),该方法不会等待您的线程完成并设置您的视图。

在线程中关闭对话框后使用 Handler.post 来收集您的信息。

handler.post(new Runnable()
{
setUpViews();
});

因此,在您的操作完成后,您的处理程序将调用您的 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.

handler.post(new Runnable()
{
setUpViews();
});

So after the your operations completed your setupViews will be called by your Handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文