android - CompoundButton.OnCheckedChangeListener() + AlertDialog -
i'm trying work multiple checkboxes inside alert dialog, made it, when click in check, code make boom!
here piece of activity (the alert dialog) extends appcompactactivity , doesn't implements nothing, it's worthy distinguish button cancel , accept working.
additionalbox.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { layoutinflater layoutinflater = layoutinflater.from(productactivity.this); final view additionalview = layoutinflater.inflate(r.layout.dialog_additionals, null); // set custom dialog components - text, buttons, accountants textview titledialog = (textview) additionalview.findviewbyid(r.id.title_additional); titledialog.settypeface(boldfont); button buttonaccept = (button) additionalview.findviewbyid(r.id.button_accept); buttonaccept.settypeface(boldfont); button buttoncancel = (button) additionalview.findviewbyid(r.id.button_cancel); buttoncancel.settypeface(boldfont); alertdialog.builder alertdialogbuilder = new alertdialog.builder(productactivity.this); alertdialogbuilder.setview(additionalview); final alertdialog alertd = alertdialogbuilder.create(); alertd.setcanceledontouchoutside(false); //fill object of additional arraylist<additionalbean> additionallist = new arraylist<additionalbean>(); additionallist.add(new additionalbean("queso", 500, false)); additionallist.add(new additionalbean("tocineta", 700, false)); additionallist.add(new additionalbean("salsas",1200,false)); listview additionallistview = (listview) additionalview.findviewbyid(r.id.list_additional); additionaladapter = new additionaladapter(productactivity.this, additionallist); additionallistview.setchoicemode(listview.choice_mode_multiple); additionallistview.setadapter(additionaladapter); additionallistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { checkbox chk = (checkbox) productactivity.this.findviewbyid(r.id.additional_check); additionalbean bean = additionallistclass.addlist .get(position); if (bean.ischecked()) { bean.setischecked(false); chk.setchecked(false); } else { bean.setischecked(true); chk.setchecked(true); } } }); //end of fill object of additional buttoncancel.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { alertd.dismiss(); } }); buttonaccept.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // quantity.settext(newquantity.gettext().tostring()); alertd.dismiss(); } }); alertd.show(); } });
then here adapter:
public class additionaladapter extends arrayadapter{ private arraylist<additionalbean> originaldata = null; private context context; public additionaladapter (context context, arraylist<additionalbean> listarray){ super(context, r.layout.additional_item); this.context = context; this.originaldata = listarray ; } public static class row { public textview labelname; public textview labelprice; public checkbox check; } @override public int getcount() { return originaldata.size(); } @override public additionalbean getitem(int position) { return originaldata.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view rowview = convertview; // reuse views if (rowview == null) { layoutinflater inflater = layoutinflater.from(context); rowview = inflater.inflate(r.layout.additional_item, null); // configure view holder row viewholder = new row(); viewholder.labelname = (textview) rowview.findviewbyid(r.id.additional_name); viewholder.labelprice = (textview) rowview.findviewbyid(r.id.additional_price); viewholder.check = (checkbox) rowview.findviewbyid(r.id.additional_check); viewholder.check.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton vw, boolean ischecked) { int getposition = (integer) vw.gettag(); originaldata.get(getposition).setischecked( vw.ischecked()); } }); rowview.settag(viewholder); } row holder = (row) rowview.gettag(); final additionalbean itm = originaldata.get(position); holder.labelname.settext(itm.getname()); holder.labelprice.settext("bs. " + string.valueof(itm.getprice())); holder.check.setchecked(originaldata.get(position).ischecked()); return rowview; }
}
the specific error is: java.lang.nullpointerexception: attempt invoke virtual method 'int java.lang.integer.intvalue()' on null object reference @ com.xetux.x_pedidos.modules.product.additionaladapter$1.oncheckedchanged(additionaladapter.java:76)
i tried this solution , doesn't work me
1: android checkbox getchecked (compoundbutton.oncheckedchangelistener (without button click event)) supposed because alert dialog.
i suspect line problem: int getposition = (integer) vw.gettag();
don't see tag set anywhere. can either set tag during binding view, or can example make position variable final - made available listener. `
Comments
Post a Comment