Android ProgressDialog does not work

Could someone tell me why my code is not working? I want to display a ProgressDialog, but the problem with the code below is that it does not appear, even when it has spent a lot of time processing the function ConsultaComercio. I have seen a lot of examples but I don't understand what I am doing wrong. I appreciate your help. Thanks in advance.

pd = ProgressDialog.show(this, "", "Loading...", true);
Toast.makeText(getApplicationContext(), "Cargando.... " + String.valueOf(numero_prueba), Toast.LENGTH_SHORT).show();

new Thread() {
    public void run() {
        try{
            // Do some Fake-Work
            ConsultaComercio();
            numero_prueba=60000;
        } catch (Exception e) { }
        // Dismiss the Dialog
        pd.dismiss();
    }
}.start();

Upvotes: 0

Views: 347

Answers (2)

Phil
Phil

Reputation: 36289

You can't update the UI from just any thread. It must be an AsyncTask.

Upvotes: 1

Andro Selva
Andro Selva

Reputation: 54322

in your onCreate() do this,

Handler handler=new Handler()
{

   public void handleMessage(Message msg)
{
   if(pd.isShowing())
{
   pd.dismiss();
}
};

and change your thread like this,

Toast.makeText(getApplicationContext(), "Cargando.... " + String.valueOf(numero_prueba), Toast.LENGTH_SHORT).show();

      new Thread() {
          public void run() {
                  try{
                          // Do some Fake-Work
                    ConsultaComercio();
                    numero_prueba=60000;
                  } catch (Exception e) { }
                  // Dismiss the Dialog
                 handler.sendEmptyMessage(0);
          }
      }.start();

Upvotes: 1

Related Questions