2

I want to do a multichoice dialog with a listener in which order I pressed the options. How can I do that?

String[] multiChoiceItems = 
getResources().getStringArray(R.array.dialog_choice_array_monday);
boolean[] checkedItems = {false, false, false, false, false, false, false};
new AlertDialog.Builder(NewScheduleActivity.this)

.setTitle(getString(R.string.main_dialog_multi_choice_monday))
.setMultiChoiceItems(multiChoiceItems, checkedItems, null)
.setPositiveButton(getString(R.string.dialog_ok), null)
.setNegativeButton(getString(R.string.dialog_cancel), null)
 .show();       
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
d0me
  • 21
  • 2
  • Did you try custom dialog? See: https://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android – nhoxbypass Nov 04 '17 at 15:52
  • @nhoxbypass The Problem is to listen to the order of the pressed option.. I don't know how to realize this – d0me Nov 04 '17 at 15:57
  • Please let me know if my solution below is what you're looking for – KJ Newtown Nov 04 '17 at 17:10
  • @KJNewtown unfortunately not... I am searching for a solution in which sequence/order I clicked the items – d0me Nov 04 '17 at 17:32
  • Yes! so you need to create a new array for the sequence, and by using my solution, in the onClick Listener, just add the code inside to add the selected elements in the sequenceArray! At the end, this array will contain the data in the order it was selected. Do you see the possibilities? – KJ Newtown Nov 04 '17 at 17:35
  • Ahh Yes. Exactly what I searched for. Thanks! @KJNewtown – d0me Nov 04 '17 at 17:40
  • Nice! I completed my post below with the updated solution – KJ Newtown Nov 04 '17 at 17:42

1 Answers1

0

This should help:

 .setMultiChoiceItems(multiChoiceItems, null, new DialogInterface.OnMultiChoiceClickListener() {

});

when you'll start typing new DialogeInterface.OnMul...... it will autoimplement the abstract method:

onClick(DialogInterface dialog, int which, boolean isChecked)

I think you can use null instead of your checkedItems array, depending if you need to use the data from this array or not.

What you need to do:

Create a new array for the sequence, and in the onClick Listener, just add the code inside to add the selected element in the sequenceArray! At the end, this array will contain the data in the order it was selected.

   String[] multiChoiceItems = getResources().getStringArray(R.array.dialog_choice_array_monday);
    ArrayList<String> sequenceArray = new ArrayList<>();
new AlertDialog.Builder(NewScheduleActivity.this)

                        .setTitle(getString(R.string.main_dialog_multi_choice_monday))
                        .setMultiChoiceItems(multiChoiceItems, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                sequenceArray.add(multiChoiceItems[which])
                            }
                        }
                        .setPositiveButton(getString(R.string.dialog_ok), null)
                        .setNegativeButton(getString(R.string.dialog_cancel), null)
                        .show();
            }

Android website: https://developer.android.com/reference/android/content/DialogInterface.html

KJ Newtown
  • 328
  • 1
  • 16