Pregunta

I want to implement AlertDialog.Builder selected items click event. Below is what I have tried so far. I'm quite new to Android and I'm not sure how to access that event. How to implement the click event for each individual item in the list?

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MakeCallAlertDialog {

    public static AlertDialog.Builder getAlertDialog(String strArray[],
            String strTitle, Activity activity) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle(strTitle);
        alertDialogBuilder.setItems(strArray, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int arg) {
                // TODO Auto-generated method stub
            }
        });

        return alertDialogBuilder;
    }
}
¿Fue útil?

Solución

Since you assigned an OnClickListener specific to that method, the int parameter is the position in the list:

Parameters

dialog The dialog that received the click.

which The button that was clicked (e.g. BUTTON1) or the position of the item clicked

This means inside your method, you should be able to do this:

public static AlertDialog.Builder getAlertDialog(final String strArray[],
        String strTitle, final Activity activity) {

    AlertDialog.Builder alertDialogBuilder =  
            new AlertDialog.Builder(activity);
    alertDialogBuilder.setTitle(strTitle);

    alertDialogBuilder.setItems(strArray,
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
           Toast.makeText(activity, strArray [which], Toast.LENGTH_SHORT).show();

           //rest of your implementation
        }
    });
   return alertDialogBuilder;
}

Otros consejos

in onClick() event use switch statement to write click method for each button.

    @Override
        public void onClick(DialogInterface dialogInterface, int arg) {
            // TODO Auto-generated method stub
            switch (arg) {
               case 0:
                  //you code for button at 0 index click
                  break;
               case 1:
                  //you code for button at 1 index click
                  break;
               default:
                    break;
            }
        }

Here, arg indicates the index of the button pressed. you can also access that button using strArray[arg]

Check my answer below if you are using single choice item selected for the strArray: Try this code

int selectedItem = 0;

// here take TempSelectOneTypeList = strArray

 AlertDialog.Builder alt_bld = new AlertDialog.Builder(
                                    Activity_Form_Data.this);
                            alt_bld.setTitle("Select One");
                            selectedItem = 0;
                            for (int j = 0; j < TempSelectOneTypeList.length; j++) {

                                if (txt_sub_lable2
                                        .getText()
                                        .toString()
                                        .equals(TempSelectOneTypeList[j].toString())) {
                                    selectedItem = j;
                                }
                            }

                            Log.i(TAG, "Selected Item is " + selectedItem);

                            alt_bld.setSingleChoiceItems(
                                    ArraylistSelectOneTypeList.get(selected),
                                    selectedItem,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int item) {
                                            selectedItem = item;
                                            // you can ocde here for the perticular selected item
                                        }
                                    });
                            alt_bld.setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {

                                            txt_sub_lable2
                                                    .setText(""
                                                            + TempSelectOneTypeList[selectedItem]
                                                                    .toString());
    }
                                    });
                            alt_bld.setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();

                                        }
                                    });

                            AlertDialog alert = alt_bld.create();
                            alert.show();

Hope it will solve your problem

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top