0

I am trying to run a progress dialog while an other process is running, and I can't do it.

the process that I want run is this:

public void Onclick_editar (View view)
{
    float porc;
    String repre;
    try{
        porc = Float.parseFloat(edit_porcentaje.getText().toString());
        repre = edit_representante.getText().toString();
        try
        {
            Hilo hilo;
            hilo = new Hilo(2, "UPDATE eleccion SET porcentaje="+porc+", delegados='"+repre+"' WHERE id="+idd );
            hilo.start();
            while(hilo.isAlive()){}

            hilo = new Hilo(4, idd);
            hilo.start();
            while(hilo.isAlive()){}
            Toast.makeText(this, "¡Actualizada con éxito!", Toast.LENGTH_SHORT).show();

        }
        catch(SQLException e)
        {
            Toast.makeText(this, "Error: Fallo en la base de datos", Toast.LENGTH_SHORT).show();
        }                   
    }catch(Exception e)
    {
        Toast.makeText(this, "Rellena todos los campos", Toast.LENGTH_SHORT).show();
    }
}

this is launching when I click a button. thanks!

EDIT/////////////

well I solved the problem with this code!!

public void button(View v) {

     new waiting().execute();

}

  class waiting extends AsyncTask<String, Void, Boolean> {

      ProgressDialog progressdialog;
      protected void onPostExecute(Boolean result) {
             super.onPostExecute(result);
             progressdialog.dismiss();

         }

      protected void onPreExecute() {
          super.onPreExecute();
          // Shows Progress Bar Dialog and then call doInBackground method
          progressdialog = new ProgressDialog(Tab_adminver_modificar_eleccion.this);
          progressdialog.setTitle("Processing....");
          progressdialog.setMessage("Please Wait.....");
          progressdialog.setCancelable(false);
          progressdialog.show();         
      }
    @Override
    protected Boolean doInBackground(String... params) {
        try {
                                Thread.sleep(5000);

                            } catch (Exception e) {

                            }
        return null;
    }

  }

but I don't use a toast in a method doInBackground (FC)

  • pls post the code that you tried and didn't run as expected, progess dialog's purpose essentially is mentioned in your question, can refer [a default progress dialog example](http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/) , [possible duplicate](http://stackoverflow.com/questions/9814821/show-progressdialog-android) – Pararth Nov 19 '14 at 09:49
  • sorry, i don't undestand you, I speak english a bit – Aitor Ramos Pajares Nov 19 '14 at 09:58
  • try to run progress dialog -- post that code in question, refer the links in above comment – Pararth Nov 19 '14 at 10:04
  • With assumption that Hilo is class derived from Thread code like `while(hilo.isAlive()){}` or `hilo.join();` in such cases make no sense becuase these calls block current thread so there is no use from new one ... – Selvin Nov 19 '14 at 10:06

4 Answers4

1

Do you mean you are waiting for hilo to finish in this line : while(hilo.isAlive()){}?

Use AsyncTask instead, make use of its onPostExecute

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • could you show me how do it in an example please? I don't understand too much about an asyncTask, thank you – Aitor Ramos Pajares Nov 19 '14 at 09:54
  • @AitorRamosPajares Im afraid not in a short time, im working now. I will googling a little for you :) – Blaze Tama Nov 19 '14 at 09:55
  • @AitorRamosPajares this is quite simple with good explanation and source code : http://programmerguru.com/android-tutorial/android-asynctask-example/ Just comment if u have some questions – Blaze Tama Nov 19 '14 at 09:56
0

If your purpose is to show a ProgressDialog, then you can use this function for displaying a progress dialog-

public static void showProgressLoader(Context context, int duration)
{
    final ProgressDialog dialog = ProgressDialog.show(context, "", "Loading...");
    dialog.show();

    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        public void run()
        {
            dialog.dismiss();
        }
    }, duration);
}

In this finction, pass the context and duration in milliseconds for which you want to show, after that duration it will get dissmissed by itself.

amit singh
  • 1,407
  • 2
  • 16
  • 25
  • yeah, but I dont know the duration for the other process, I want that this dialog will be closed when mysql consult fnish. – Aitor Ramos Pajares Nov 19 '14 at 10:05
  • As of now you can pass 2 to 3 seconds but in that case, you have to call the dialog.dismiss() at the end of the Onclick_editar(View view) function. – amit singh Nov 19 '14 at 10:09
  • i have errors in: context cannot be resolved to a variable progressdialog cannot be resolved Splash cannot be resolved to a type – Aitor Ramos Pajares Nov 19 '14 at 10:47
0

you can try like this your process will run in background and progress dialog will be executing as well but when process will complete progress dialog will dismiss..

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

    public asynTask1 (MainActivity activiy) {
        context = activiy;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        progressdialog.dismiss(); 




    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        progressdialog = new ProgressDialog(Splash.this);
        progressdialog.setTitle("Processing....");
        progressdialog.setMessage("Please Wait.....");
        progressdialog.setCancelable(false);
        progressdialog.show();

    }

    @Override
    protected Boolean doInBackground(String... params) {

    // do process


}

U can call it onclick or also can onCreate.. it better to make separate class for asyntask1 and write new asynTask1(MainActivity.this).execute(); where you want to call it..

i hope it will helps ...

user
  • 82
  • 1
  • 7
  • have I to call this class on my onclik method? – Aitor Ramos Pajares Nov 19 '14 at 10:30
  • i have errors in: context cannot be resolved to a variable progressdialog cannot be resolved Splash cannot be resolved to a type – Aitor Ramos Pajares Nov 19 '14 at 11:45
  • have you declared Context context = this; and ProgressDialog progressdialog; ... ??? – user Nov 19 '14 at 11:55
  • thank you I have just understand the methods od preexecute, doinbackground....., but I have a force close when the process get to doinbackground, the dialog run before but after have an error. is there a problem in my code of the MySQL consult? really? I will try this with an other simple proccess – Aitor Ramos Pajares Nov 19 '14 at 12:32
0
public void launchRingDialog(View view) {

    final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Downloading Image ...", true);

    ringProgressDialog.setCancelable(true);

    new Thread(new Runnable() {

        @Override

        public void run() {

            try {

                **// the MYSql consultation should be here?**

            } catch (Exception e) {



            }

            ringProgressDialog.dismiss();

        }

    }).start();