Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setMultiChoiceItems() doesn't shows items

Im using a DialogFragment to show a Dialog with a multi choice items, it shows the Dialog but without the items, there is no checkboxes to select.

This is how i invoke the dialog:

    botAddMedicacion.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment fragAddMedic = new AnadirMedicacionFragment();
            fragAddMedic.show(getFragmentManager(), "addMedicacion");
        }
    });

And this is the AnadirMedicacionFragment code:

    final CharSequence[] items={".NET","J2EE","PHP"};

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

    medicSelec = new ArrayList();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.anadir_medicacion);


    // elementos de la lista
    builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            if (isChecked) {
                medicSelec.add(which);
            } else if (medicSelec.contains(which)) {
                medicSelec.remove(Integer.valueOf(which));
            }

        }
    });

    // boton ok
    builder.setPositiveButton(R.string.anadir, new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    // boton cancelar
    builder.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    return builder.create();
}

The item list is passed in builder.setMultiChoiceItems(items, null[...]

dialog fragment multi choose

It seems it works, but there are no checkboxes or list.

like image 744
alxsimo Avatar asked Jan 15 '14 15:01

alxsimo


1 Answers

The problem is that you have a setMessage and a setMultiChoiceListener. You can only have one of them. Try this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.anadir_medicacion);
like image 193
PearsonArtPhoto Avatar answered Oct 12 '22 23:10

PearsonArtPhoto