Saturday, January 26, 2013

Populate AlertDialog with CheckBoxes


boolean[] isChecked = null;
final  List<CharSequence> selectedItems = new ArrayList<CharSequence>();
final CharSequence[] others = {"value1","value2", "value3", "value4" };
AlertDialog.Builder buildnew = new AlertDialog.Builder(this);
buildnew .setTitle("Select ");
buildnew .setMultiChoiceItems(others,isChecked, new DialogInterface.OnMultiChoiceClickListener() {
                  public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked)
{  selectedItems.add(others[which]);
}

else
                     {
selectedItems.remove(others[which]);
}
StringBuilder stringBuilder = new StringBuilder();

               for(CharSequence item : selectedItems)
           stringBuilder.append(item + ",");
               edittext.setText(stringBuilder.toString());            
}
});
buildnew.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
                 {
dialog.dismiss();
}
});
buildnew .create();
buildnew .show();

No comments:

Post a Comment