Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog in a separate thread

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

like image 727
Alex Avatar asked Jan 29 '26 12:01

Alex


1 Answers

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(); 
    }  
};
like image 81
kvh Avatar answered Jan 31 '26 00:01

kvh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!