Event and dialog on a listview

1

I have a listview with elements, well, when clicking on one of them I should see a dialog or similar that gives two options in any way and says I accept or I do not accept and that from the code I can know which one has chosen one. How can I face this? What event should I modify and how do I add the dialog?

    
asked by Red 17.05.2016 в 18:22
source

2 answers

2

Add a listener, specifically OnItemClickListener to your ListView, when you click on an element of the ListView, you would call the dialog:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
     Log.i("Click", "click en el elemento " + position + " de mi ListView");
     muestraDialogo();

  }
});

The method to call would be the one that creates the confirmation dialog:

private void muestraDialogo(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Deseas realizar alguna acción?")
       .setCancelable(false)
       .setPositiveButton("Si", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                // Aquí lo que deseas realizar
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();
    alert.show();
}

Here you can see the documentation on how to create a dialogue using Dialog.Builder

    
answered by 17.05.2016 / 18:35
source
0

You can try

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("¿Quieres continuar?")
   .setCancelable(false)
   .setPositiveButton("Si", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            // tu código
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
alert.show();
    
answered by 17.05.2016 в 18:26