ProgressDialog help

0

I'm looking for a way to have a progressBar or launch a progressDialog and it will disappear when all functions are finished.

I have the saveItems function in which other functions are executed, I think that's where the problem comes from.

progressDialog.onStart();

        guardarItems("ATAQUE");
        guardarItems("DEFENSA");
        guardarItems("MAGICO");
        guardarItems("MOVIMIENTO");
        guardarItems("JUNGLA");


progressDialog.dismiss();






public void guardarItems(final String tipo){

    final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Items");
    Query q = dbRef.orderByChild("tipo").equalTo(tipo);
    final ArrayList<Item> listaItem = new ArrayList<Item>();
    Log.d("prueba", tipo);
    q.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    for (DataSnapshot datasnapshot : dataSnapshot.getChildren()) {
                                        //De cada nodo producto, obtenemos un objeto de este
                                        Item item = datasnapshot.getValue(Item.class);
                                        //Toast.makeText(visualizarPedidos.this, ""+p.getProductos().get(0).getNombreProducto(), Toast.LENGTH_SHORT).show();
                                        listaItem.add(item);
                                        descargarImagen(item.getId());
                                    }

                                    File localFile = null;
                                    try {
                                        localFile = getAbsoluteFile(tipo+".dat",getApplicationContext());
                                        if (!localFile.exists()) {
                                            if (!localFile.createNewFile()) {
                                                Log.d("prueba", "Unable to create file");
                                                throw new IOException("Unable to create file");
                                            }
                                            Log.d("prueba", "Create file");

                                            FileOutputStream fileout = new FileOutputStream(localFile);
                                            ObjectOutputStream out = new ObjectOutputStream(fileout);
                                            out.writeObject(listaItem);
                                            out.close();

                                            Log.d("prueba", String.valueOf(localFile));
                                        }


                                    } catch (Exception ex) {

                                    }


                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {


                                }
                            }
    );

}



public void descargarImagen(int img) {

        StorageReference stoRef = FirebaseStorage.getInstance().getReference().child("items/item" + img + ".jpg");


        File localFile = null;


        try {
            //localFile = new File(Environment.getExternalStorageDirectory(), "item"+img+".jpg");//funciona

            localFile = getAbsoluteFile("item" + img + ".jpg", this);
            //localFile = new File(getFilesDir(), "item"+img+".jpg");//no funciona
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (!localFile.exists()) {
            Log.d("prueba", "no existe ");
            stoRef.getFile(localFile)
                    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            Log.d("prueba", " guardado ");
                        }
                    }).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.d("prueba", "no guardado ");
                }
            });
        }else{Log.d("prueba", "existe");}

}


private File getAbsoluteFile(String name, Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.d("prueba", "visto "+name);
        return new File(context.getExternalFilesDir(null), name);
    } else {
        return new File(context.getFilesDir(), name);
    }
}
    
asked by SauMert 13.12.2018 в 21:39
source

1 answer

1

To show your ProgressDialog you must use the method .show ()

progressDialog.show();

to stop displaying the .dismiss () method:

progressDialog.dismiss();

Therefore, if your saveItems () method performs a synchronous task, it is sufficient with:

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMax(100);
progressDialog.setMessage("Procesando....");
progressDialog.setTitle("SauMert aplicación");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();

    progressDialog.show();

            guardarItems("ATAQUE");
            guardarItems("DEFENSA");
            guardarItems("MAGICO");
            guardarItems("MOVIMIENTO");
            guardarItems("JUNGLA");


    progressDialog.dismiss();
  • You should know that if the process that calls the method guardarItems() is done quickly, you might not see the ProgressDialog since it would appear and disappear.
answered by 13.12.2018 в 23:37