我使用DialogFragment显示带有多项选项的Dialog,它显示Dialog但没有项目,没有要选择的复选框.
这是我调用对话框的方式:
botAddMedicacion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment fragAddMedic = new AnadirMedicacionFragment(); fragAddMedic.show(getFragmentManager(),"addMedicacion"); } });
这是AnadirMedicacionFragment代码:
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,int which) { } }); return builder.create(); }
项目列表在builder.setMultiChoiceItems中传递(items,null […]
它似乎有效,但没有复选框或列表.
解决方法
问题是你有一个setMessage和一个setMultiChoiceListener.你只能拥有其中一个.试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.anadir_medicacion);