2

I have a procedure that extracts data from a database and populates it to the list. I want to display progress dialog box while query is executed, but it visually appears only after the query is executed. I believe I have to run a ProgressDialog in a separate thread, but followed few suggestions and could not make it work.

So in my Activity I just have

private void DisplayAllproductListView(String SqlStatement) {           
     ProgressDialog dialog = 
     ProgressDialog.show(MyActivity.context, "Loading", "Please wait...", true);
     //..................
     //..................
     //execute sql query here
     dialog.dismiss();
   }

thanks

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
Alex
  • 81
  • 1
  • 2
  • 10

3 Answers3

11

1.show your process dialog in main thread

2.start a new thread (such as Thread A) to process your heavy job

3.when done, use handler to send a message from Thread A to main thread, the latter dismisses the process dialog

code like this private ProcessDialog pd;

private void startDialog()
{

 pd = ProgressDialog.show(MainActivity.this, "title", "loading");  

     //start a new thread to process job
     new Thread(new Runnable() {  
         @Override  
         public void run() {  
             //heavy job here
             //send message to main thread
             handler.sendEmptyMessage(0); 
          }  
      }).start(); 
}

Handler handler = new Handler() {  
    @Override  
    public void handleMessage(Message msg) {
        pd.dismiss(); 
    }  
};
Samjack
  • 190
  • 1
  • 1
  • 11
kvh
  • 2,118
  • 19
  • 29
  • Thank you very much for your solution. I think it is perfectly working except it crashes for me. The thing is that my "heavy job" includes query , customised data adaptor and mapping to list view. Do you think it could be the problem?? should I have query only in a new tread?? – Alex Jul 19 '13 at 09:06
4

Try something like this:

private class MyAwesomeAsyncTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog mProgress;

    @Override
    protected void onPreExecute() {
        //Create progress dialog here and show it
    }

    @Override
    protected Void doInBackground(Void... params) {

        // Execute query here

        return null;

    }  

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //update your listView adapter here
        //Dismiss your dialog

    }

}

To call it:

new MyAwesomeAsyncTask().execute(); 
Adrián Rodríguez
  • 1,836
  • 15
  • 16
  • Thank you very much for your solution. I think it is perfectly working except it crashes for me. The thing is that my "heavy job" includes query , customised data adaptor and mapping to list view. Do you think it could be the problem?? should I have query only in a async task?? – Alex Jul 19 '13 at 09:06
  • try to put the code of the adapter and listview update in the onPostExecute method. You can't manipulate the UI thread from the doInBackground method, as it runs in background :) – Adrián Rodríguez Jul 19 '13 at 09:21
  • Thanks again. query and prepare of custom list is working finest in doInBackground. how can I pass an object CustomListDataAdaptor from doInBackground to onPostExecute?? what is the syntax? thanks a million – Alex Jul 19 '13 at 09:57
  • As an asynctask should be an inner class of your main class, simply declare the CustomListDataAdapter as a field of your main class. – Adrián Rodríguez Jul 19 '13 at 10:03
  • Just figured it out. thanks a lot!!!! Last one how to pass a string (my sql query) as a parametr to doInbackground? – Alex Jul 19 '13 at 10:08
0

All you need to do, is to tell Android to run it on the main UI thread. No need to create a Handler.

runOnUiThread(new Runnable() {
    public void run() {
        progressDialog.dismiss();
    }
});
Daniele Testa
  • 1,538
  • 3
  • 16
  • 34