android - Implementing Dialog box with checkboxes -


here dialog box

 

public class customdialogclass extends dialog implements  android.view.view.onclicklistener {       public activity c;      public dialog d;      public button no;      checkbox yes;      boolean p;      public customdialogclass(activity a) {          super(a);          // todo auto-generated constructor stub          this.c = a;      }       @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          requestwindowfeature(window.feature_no_title);          setcontentview(r.layout.custom_dialog);          yes = (checkbox) findviewbyid(r.id.checkbox1);          no = (button) findviewbyid(r.id.btn_no);          no.setonclicklistener(this);          yes.setchecked(false);          yes.setonclicklistener(this);      }       @override      public void onclick(view v) {          switch (v.getid()) {          case r.id.checkbox1:              yes.setchecked(true);              break;          case r.id.btn_no:              dismiss();              break;          default:              break;          }      }  } 

now, open dialog , check checkbox, click on button , dismiss dialog. when open again, checkbox again unchecked. should do?

the way using dialog not recommended anymore! should consider using dialogfragment.

the problem why loosing checked state because dialog recreated when open again.

see example of dialogfragment approach http://developer.android.com/reference/android/app/dialogfragment.html. see can pass arguments dialog.

for solution recommend when close dialog pass selected value hosting activity... when dialog should reopenned pass arguments dialog dialog sets default selection.

edit:

  1. since display checkboxes take , adapt example code http://developer.android.com/guide/topics/ui/dialogs.html#addingalist checkboxes. it's still convenient use alertdialog.builder.

  2. wrap builder dialogfragment overwriting oncreatedialog-method. can pass list of selected items via bundle boolean array , use setmultichoiceitems.

    public class checkboxalertdialogfragment extends dialogfragment {     public static checkboxalertdialogfragment newinstance(boolean[] checkeditems) {         checkboxalertdialogfragment frag = new checkboxalertdialogfragment();         bundle args = new bundle();         args.putbooleanarray("checkeditems", checkeditems);         frag.setarguments(args);         return frag; }  @override public dialog oncreatedialog(bundle savedinstancestate) {     final boolean[] checkeditems = getarguments().getbooleanarray("checkeditems");      alertdialog.builder builder = new alertdialog.builder(getactivity());     // set dialog title     builder.settitle(r.string.pick_toppings)             // specify list array, items selected default (null none),             // , listener through receive callbacks when items selected             .setmultichoiceitems(r.array.toppings, checkeditems,                     new dialoginterface.onmultichoiceclicklistener() {                         @override                         public void onclick(dialoginterface dialog, int which,                                             boolean ischecked) {                             checkeditems[which] = ischecked;                         }                     })             // set action buttons             .setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int id) {                     // user clicked ok, save checkeditems results somewhere                     // or return them component opened dialog                     //...                 }             })             .setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int id) {                     //...                 }             });      return builder.create(); } } 
  3. in activity should have variable, e.g. named checkeditems, of type boolean[] somewhere saves checkbox states. can open dialog with:

    void showdialog() {         dialogfragment newfragment = checkboxalertdialogfragment.newinstance(checkeditems);         newfragment.show(getfragmentmanager(), "dialog tag");     } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -