显示ProgressDialog同时等待接合螺纹 [英] Displaying a ProgressDialog while waiting for a joined Thread

查看:127
本文介绍了显示ProgressDialog同时等待接合螺纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动,我打开了从数据库列表中的内容,并希望显示ProgressDialog而it's加载。
我得到了两个工作it's自己,但如果我加载在一个线程中的数据(这是我应该做的?),it's数据加载之前显示在列表中。但是,如果我用加盟,ProgressDialog犯规,甚至出现。

In my Activity, I load the content for a list from a DB, and want to display a ProgressDialog while it´s loading.
I got both working on it´s own, but if I load the data in a thread (which I should do?), the list is displayed before it´s data is loaded. But if I use join, the ProgressDialog doesnt even appear.

我怎么能结合起来?或者,这根本不可能使用线程? (AsyncTask的可能?)

How can I combine this? Or is this not possible at all with threads? (AsyncTask maybe?)

Here's的code,以供参考:

Here´s the code for reference:

    final ProgressDialog progressD=ProgressDialog.show(ShopSwitchActivity.this, "", "Loading..", true);

    Thread myThread = new Thread(new Runnable() {  
        @Override
        public void run() {
          try
          {
              getData();
          }catch(Exception e){}
        }
    });
    myThread.start();

    try {
            myThread.join();
    } catch (InterruptedException e) {
    }
    progressD.dismiss();

编辑:更新code与AsyncTask的:

Updated Code with AsyncTask:

public class LoadList extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;
    ShopSwitchActivity activity;

    public LoadList(ShopSwitchActivity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity);
    }

    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();
    }

        @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected Boolean doInBackground(final String... args) {
        try{
       activity.getData();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
       return true;
    }
}

编辑:我的解决方案 现在使用的AsyncTask加载数据,和it's完成后,我刷新列表以新的数据。

My Solution Using AsyncTask now to load the Data, and after it´s done, I refresh the list with the new data.

推荐答案

您可以用AsyncTask的做到这一点。写的AsyncTask类,你想要做你的业务主类。你可以创建你的异步类的preexcecute进度对话框,并驳回异步类onpostexecute。这里是你将如何做到这一点:

You can do it with AsyncTask. Write AsyncTask class in your main class that you want to do your operations. You can create the progress dialog in preexcecute of your async class and dismiss in onpostexecute of async class. Here is how you will do this:

  class MyAsync extends AsyncTask<String, Void, Void> {
     ProgressDialog pd;
     Context co;
     MyActivity ma;

     public MyAsync (MyActivity ma){
         this.ma= ma;
         this.co = ma;
         pd= new ProgressDialog(co);
     }

     @Override
     protected void onPreExecute() {
         this.pd.show();

     super.onPreExecute();
     }
            @Override
            protected Void doInBackground(String... params) {
                // do your database operations here
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                // show db results and dismiss progress dialog pd.dismiss();
                super.onPostExecute(result);
            }
        }

在MyActivity电话为:

in MyActivity call as :

MyActivity ma = this;
new MyAsync(ma).execute();

这篇关于显示ProgressDialog同时等待接合螺纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆