3

I am using an alert dialog that shows a menu on the beginning of the app, I want the dialog to show me 2 values which are "name"s from an object, here is the code of the alert dialog:

public void showDialog() {

    EntityType en = new EntityType();
    ArrayList array = ApplicationController.entities;

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick one");
    builder.setItems(array, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // I want to write my code here
        }
    });

    builder.show();
}

EntityType is my object that contain a string "name" and ApplicationController.entities contains the array

Sufian
  • 6,405
  • 16
  • 66
  • 120

4 Answers4

3

Try the below code.. it works for me... :)

AlertDialog.Builder builderSingle = new AlertDialog.Builder(MainActivity.this);
    builderSingle.setIcon(R.drawable.green_tick_add);
    builderSingle.setTitle("Choose..");

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_item);
    arrayAdapter.add("Change Photo");
    arrayAdapter.add("Remove Photo");

    builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String strName = arrayAdapter.getItem(which);

        }
    });
    builderSingle.show();
Nihas Nizar
  • 619
  • 8
  • 15
2
public void showDialog() {

    EntityType en = new EntityType();
    ArrayList array = ApplicationController.entities;

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick one");
    ArrayList<String> displayValues=new ArrayList<>();
      for (Entity entity : array) {
         displayValues.add(entity.name);
      }

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,array);
      final AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Pick one");
      builder.setSingleChoiceItems(displayValues, 0, new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            Entity selectedItem = array[which];
         }
      });    


    builder.show();
}
georgeok
  • 5,321
  • 2
  • 39
  • 61
1
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.select_dialog);
adapt.add("your entity name");

then edit diaolog code :

builder.setAdapter(adapt, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            //write code here
        }
    });
0

You have to create an ArrayAdapter holding your EntityType names and adding them to it:

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
adapt.add("your entity name");

Then on the dialog:

builder.setAdapter(adapt, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            //Your code here
        }
    });

Hope it helps

Julián Martínez
  • 3,054
  • 3
  • 19
  • 17