Checks Alert Dialog Android

2

I am trying to do some checks within alertdialog to see if certain conditions are met and let the user continue with the process. But when the user gives the download button (PositiveButton) makes the checks and leaves the alertdialog are met or not, when what I want is that if you meet any condition do not close the alertdialog .

The alertdialog is the following:

And the code of alertdialog is:

dialog.setTitle(getResources().getString(R.string.calendar_dialog_info));
dialog.setView(layout);
dialog.setCancelable(false);

dialog.setPositiveButton(getResources().getString(R.string.download), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

        KeyValueArrayAdapter.KeyValue sel = (KeyValueArrayAdapter.KeyValue) spinner_cal.getSelectedItem();

        String cuentaSel = sel.value;
        long idsel = Long.parseLong(sel.key);

        if(idsel != -1) { 
            if (Utiles.conexionDisponible(getApplicationContext())) {
                String tipo = Constants.CALENDARIO_ALUMNO;//TODO saber que tipo hay que descargar
                //pasar id calendario y tipo de calendario
                ObtenerCalendario obtenerTask = new ObtenerCalendario( tipo, idsel);
                obtenerTask.execute();
            } else {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_conex), Toast.LENGTH_LONG).show();
            }

            dialog.cancel();
        }else{
            Toast.makeText(PantallaPersonalActivity.this, "No se ha seleccionado nada " , Toast.LENGTH_SHORT).show();
        }
    }
});
dialog.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // Cancelar
        dialog.cancel();
    }
});

dialog.show();

EDITED :

The expected behavior is that when you click the download button, check if a calendar has been selected, if you have selected close the alertdialog and call a AsyncTask , but if not nothing has been selected I do not want the alertdialog to be closed and show either a Toast , SnackBar or in the same alertdialog that a calendar selects.

    
asked by Joacer 19.10.2016 в 10:04
source

1 answer

2

After taking a look at the advice of @Maguz I found a solution to what I wanted to do, I chose to create a alertdialog with a unique selection of items (Radios) with the function setSingleChoiceItems() leaving the code of the following way:

final KeyValueArrayAdapter calendarios = CalendarUtils.obtenerCalendars(PantallaPersonalActivity.this);

AlertDialog alert = new AlertDialog.Builder(this)
    .setTitle(R.string.calendar_dialog_info)
    .setCancelable(false)
    .setSingleChoiceItems(calendarios.getEntries(), -1, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            String id = calendarios.getEntryValue(which);
            String nombre = calendarios.getEntry(which);

            long idsel = Long.parseLong(id);

            Log.i("calendario","cuenta sel: '" + nombre+ "' id: '"+ idsel+"'");

            if (Utiles.conexionDisponible(getApplicationContext())) {
                String tipo = Constants.CALENDARIO_ALUMNO;//TODO saber que tipo hay que descargar

                //pasar id calendario y tipo de calendario
                ObtenerCalendario obtenerTask = new ObtenerCalendario( tipo, idsel);
                obtenerTask.execute();
            } else {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_conex), Toast.LENGTH_LONG).show();
            }

            dialog.dismiss();
        }
    })
//  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//
//      @Override
//      public void onClick(DialogInterface dialog, int which) {

//      }
//  })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    }).create();

    alert.show();

Capture how the alertdialog is with this code:

In this way I force the user to select a calendar or cancel the action, avoiding checking that he has not selected any calendar, since when he selects one he will continue with the calendar download process.

    
answered by 21.10.2016 / 16:41
source