Android Should I use a doInBackground inside an AlertDialog?

2
Good morning everyone. I have this doubt ...

The fact is that I need to click on an item in a list to 'execute' a URL that returns a true or false answer, to confirm or cancel a reservation. I am using a AlertDialog within a OnItemClickListener to accept or cancel the confirmation of the reservation.

What I was thinking was to use a doInBackground within AlertDialog . Somehow it is not convenient to do this? Is there a viable alternative that you recommend for this case? I hope I have been able to express myself well. From already thank you very much.

This is my OnItemClickListener:

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Toast.makeText(this,
            "Click en posición: " + position + " idt: " + listaTurnosLibres.get(position).get("idt"),
            Toast.LENGTH_LONG).show();

    //ALERTDIALOG "¿Confirma sacar turno?".
    AlertDialog.Builder builder = new AlertDialog.Builder(TurnosLibres.this);
    builder.setMessage(R.string.dialog_sacar_turno)
            .setCancelable(false)
            .setPositiveButton(R.string.dialog_aceptar, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //Aceptar
                }
            })
            .setNegativeButton(R.string.dialog_cancelar, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //Cancelar
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

EDIT:

I solved it by doing the following, I put the AsyncTask within the OnItemClickListener and in the AlertDialog if it confirms I execute the class TakeTurn (AsyncTask) and if not, it does not do anything. It was perfect.

    
asked by L_C 12.08.2017 в 07:15
source

1 answer

1

A AsyncTask in this case would be used to click (within OnItemClickListener ) to an element of the list, you can perform the task of making a request to the url and get a result, this without blocking the main thread.

When you get a result AsyncTask using your onPostExecute() method, you can perform, depending on of the result the creation of the Dialogue:

 protected void onPostExecute(Long result) {
     ...
     muestraDialogo();
     ...
 }
  

Should I use a doInBackground inside an AlertDialog?

This is incorrect, it can be done, but the purpose of onPostExecute() is to be able to show the answer in the UI based on the result of the task.

The correct thing is to show your AlertDialog when making the request, obtain a result in onPostExecute() and based on that, decide to show or not the AlertDialog to confirm or cancel a reservation.

    
answered by 13.08.2017 / 22:28
source