Benjamin Schwalb
Benjamin Schwalb

Reputation: 1134

Displaying a ProgressDialog while waiting for a joined Thread

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.

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

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();

EDIT: 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;
    }
}

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

Upvotes: 1

Views: 4022

Answers (4)

canova
canova

Reputation: 3975

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);
            }
        }

in MyActivity call as :

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

Upvotes: 4

user2060635
user2060635

Reputation: 261

private Thread myThread;
private ProgressDialog mProgDialog;

mProgDialog = ProgressDialog.show(ShopSwitchActivity.this,"","Laden..", true);

myThread= new Thread(new Runnable() 
{
    public void run() 
    {
    myThread.setPriority(Thread.MIN_PRIORITY);

    try
    {
        getData();
    }catch(Exception e){}

    runOnUiThread(new Runnable() 
    {
        public void run() 
        {
        if (mProgDialog != null&& mProgDialog.isShowing())
        mProgDialog.dismiss();
        }                                                                               }
    });

    }
});
myThread.start();

Upvotes: 1

g00dy
g00dy

Reputation: 6778

You should use AsyncTask instead actually.

Here is the link to the library. It is fairly simple:

1) onPreExecute() = show ProgressDialog

2) doInBackground() = execute your code

3) onPostExecute() = dismiss ProgressDialog

Here's a nice tutorial too.

In general:

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(this.context);
    dialog.setMessage("Loading...");
    dialog.setCanceledOnTouchOutside(false);
}

@Override
protected void onPostExecute(String result) {
    if(dialog.isShowing()) {
        dialog.dismiss();
    }
}

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93668

You seem to miss the point of a thread. A thread occurs at the same time as your application. So your app doesn't call start then wait for the thread to be over- if it did you could just use a function. Instead your code continues to run. So if you just call join immediately, you're not doing anything. You'd get around a NetworkOnMainThreadException this way, but you'd still hold up the UI thread making your app totally non-responsive (and as a result not showing the dialog), and you'd eventually crash when a watchdog timer kills you.

Instead, the best way to handle this is to use an AsyncTask. Call getData in doInBackground(). Then dismiss the dialog in onPostExecute.

Upvotes: 1

Related Questions