Nicolas400
Nicolas400

Reputation: 653

How to update progressBar in ANDROID from another method in another class

i'm newby in Android and I want to give the user a more professional look and feel.

I have a process that export data to Excel file, but it takes too much time.

I trigger the method like this:

  ...  
                  // TODO Auto-generated method stub
                    System.out.println(confirm);
                    if (confirm){

                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                ensayoController.exportarEnsayoExcel(list); //<- this takes from 2 to 3 minutes!
                                for (Ensayo e: list){
                                    Toast.makeText(ExportaEnsayosActivity.this, "Ensayo " + e.getDescripcion() + " exportado!", Toast.LENGTH_LONG);
                                    System.out.println("Ensayo " + e.getDescripcion() + " exportado!");
                                }
                                AlertDialog alertDialog = new AlertDialog.Builder(ExportaEnsayosActivity.this).create();
                                alertDialog.setTitle("Atencion!");
                                alertDialog.setMessage("Finalizó la exportacion!");
                                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        });
                                alertDialog.show();
                            }
                        }).run();

                    }
...

I added a message at the end to let the user knows when the task is finished.

I have 2 problems; 1) Toast never shows a message. 2) I want to create and update a progress bar from within "exportarEnsayoExcel" method.

Do I have to add a new parameter with the progressBar UI Element ?

Or the approach is different.

Best Regards

Upvotes: 0

Views: 331

Answers (2)

Use AsyncTask, there are method

doInBackground/// do something job in backgorund,call publishProgress

onProgressUpdate/// update ui

Upvotes: 1

Jaskaran Singh
Jaskaran Singh

Reputation: 157

You are creating toast on a separate thread and you are not showing it using show() method. Second, there's no code for showing the toast message at the end of your process you are just creating a toast when iterating over a list. Create a toast like this in a separate thread.

ExportaEnsayosActivity.this.runOnUiThread(new Runnable() {
   Toast.makeText(ExportaEnsayosActivity.this, "Your message", Toast.LENGHT_SHORT).show();
}) 

If you are using a service for processing your list to create an excel you can send a broadcast receiver and send a broadcast to the activity with extra value that contains completion status of the process. In your activity listen for broadcast and update the progress bar with that value. You can see more about broadcasts over here Broadcasts overview. The second option is if you are using RxJava for your project you can create an event bus which will be then used to send events to your activity from your ensayoController class. You can read more about the event bus here Super simple event bus using RxJava2.

Upvotes: 0

Related Questions