implementation of ProgressDialog

0

I want to implement PROGRESSDIALOG but it does not work for me. Somebody can help me and tell me what I'm doing wrong. Here is the procedure where I want to show you!

public boolean onOptionsItemSelected(MenuItem item) {

    progressDialog.setMessage("Registrando Datos");
    progressDialog.setCancelable(false);
    progressDialog.show();

    int id = item.getItemId();
    if (id == R.id.ItemSincro) {

        if (!CompruebaConexion(this)) {
            Toast.makeText(getBaseContext(),"No se ha podido establecer conexion con internet", Toast.LENGTH_SHORT).show();
        }else{

            int CantDatos=recyclerS.getAdapter().getItemCount();//MOSTARNDO CANTIDAD DE DATOS DEL RECYCLER
            int contador = 0;
            for(int i = 0; i < CantDatos;i++){

                SincronizarDatos(
                        DateList.get(contador).getNombre(),
                        DateList.get(contador).getApellido(),
                        DateList.get(contador).getEdad(),
                        DateList.get(contador).getGenero(),
                        DateList.get(contador).getDeporte(),
                        "sincronizado"
                        );
                contador++;
            }
            ActualizarEstado();
            ListarDatosSincro();
            AdaptadorRecy Radapter = new AdaptadorRecy(DateList);
            recyclerS.setAdapter(Radapter);
            Toast.makeText(getApplicationContext(),"REGISTROS CARGADOS CORRECTAMENTE",Toast.LENGTH_SHORT).show();
        }
    }
    progressDialog.dismiss();
    return super.onOptionsItemSelected(item);

}
    
asked by CarlosVilla 15.04.2018 в 08:59
source

1 answer

0

It seems to me that the progressDialog is not shown because the task is carried out too quickly or because you are doing the loading of data in the main thread ... I have tried it and if you do the task in another thread that is not The Main and when you finish calling the dismiss () method works.

private ProgressDialog progressDialog;

private void onButtonClicked(){
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("registrando datos");
    progressDialog.setCancelable(false);
    progressDialog.show();
    tarea = new MiTareaAsincrona();
    tarea.execute();
}

private class MiTareaAsincrona extends AsyncTask<Void, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {

        for(int i=1; i<=10; i++) {
            cargarDatos();
        }

        return true;
    }


    @Override
    protected void onPostExecute(Boolean result) {
        if(result)
            progressDialog.dismiss();
    }
}

pd: the progressDialog is already obsolete, I recommend you look for other alternatives.

    
answered by 15.04.2018 / 23:23
source