Friday, 15 August 2014

java - How can i get checked checkboxes values from checkbox when checkbox made programmatically in android -



java - How can i get checked checkboxes values from checkbox when checkbox made programmatically in android -

i making check-boxes programmatically. , want value of checked check-boxes when click on button... how can this....

i getting value of checkboxes database.

here working code..

public class loyaltyprogram extends activity { sessionmanager session; edittext mpv, discount; button save; string finalresult, getflag, statusemp, accessname, emp_id, bus_id, preferences, bus_type_id, emp_access_name, responsestring, success, name, id, pref, per_amout, percentage, datediff, dateno, minpvalue , discountonit; toast tag; // flag net connection status boolean isinternetpresent = false; // connection detector class connectiondetector cd; progressdialog pdialog; int a; string[] idsplit, namesplit, prefsplit; list<string> testarraylist; linearlayout llmain; linearlayout[] llayout; integer count1 = 0; context mcontext; textview tvo; checkbox cb; stringbuffer result = new stringbuffer(); @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.loyaltyprogram); tvo = (textview) findviewbyid(r.id.tvother); mpv = (edittext) findviewbyid(r.id.et_minpurchase); discount = (edittext) findviewbyid(r.id.et_discount); isinternetpresent = cd.isconnectingtointernet(); if (isinternetpresent) { new homedatanew().execute(); llmain = (linearlayout) findviewbyid(r.id.linearlayoutmain); } else { toast toast = toast.maketext(getapplicationcontext(), "check net connection", toast.length_long); toast.setgravity(gravity.center, 0, 0); toast.show(); } save = (button) findviewbyid(r.id.button); save.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub minpvalue = mpv.gettext().tostring(); discountonit = discount.gettext().tostring(); for(int t = 0; t<idsplit.length; t++) { if(cb.ischecked()) { system.out.println(idsplit[t]); } } } }); } class homedatanew extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(loyaltyprogram.this); pdialog.setmessage("loading data.."); pdialog.setcancelable(false); pdialog.show(); } protected string doinbackground(string... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost( "http://10.0.2.2/amardeep/android_api/checkbox.php"); seek { session = new sessionmanager(getapplicationcontext()); // user info session hashmap<string, string> user = session.getuserdetails(); // id bus_id = user.get(sessionmanager.key_b_id); bus_type_id = user.get(sessionmanager.key_b_type_id); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>( 2); namevaluepairs.add(new basicnamevaluepair("bus_type_id", "bt101")); namevaluepairs.add(new basicnamevaluepair("bus_id", "b101")); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); response.getstatusline().getstatuscode(); httpentity getresponseentity = response.getentity(); responsestring = entityutils.tostring(getresponseentity); } grab (clientprotocolexception e) { // todo auto-generated grab block } grab (ioexception e) { // todo auto-generated grab block } homecoming responsestring; } @suppresslint("newapi") protected void onpostexecute(string resultstr) { seek { jsonobject json = new jsonobject(responsestring); jsonarray jarray = json.getjsonarray("customer"); (int = 0; < jarray.length(); i++) { jsonobject json_data = jarray.getjsonobject(i); success = json_data.getstring("success"); id = json_data.getstring("id"); name = json_data.getstring("name"); pref = json_data.getstring("pref"); per_amout = json_data.getstring("per_amount"); percentage = json_data.getstring("percentage"); datediff = json_data.getstring("datediff"); dateno = json_data.getstring("dateno"); if (percentage.equals("null")) { percentage = ""; } if (per_amout.equals("null")) { per_amout = ""; } if((datediff.equals("no"))&&(dateno.equals("no"))) { count1 = 1; } else if((datediff.equals("yes"))&&(dateno.equals("no"))) { count1 = 2; } else { count1 = 0; } idsplit = id.split(","); = idsplit.length; namesplit = name.split(","); prefsplit = pref.split(","); testarraylist = new arraylist<string>( arrays.aslist(prefsplit)); } } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } if (success.equals("1")) { mpv.settext(per_amout); discount.settext(percentage); if ((count1.equals(1)) || (count1.equals(2))) { mpv.setenabled(true); discount.setenabled(true); } else { mpv.setenabled(false); discount.setenabled(false); save.setenabled(false); } int b = (a / 5); int c = (a % 5); if (c != 0) { b = b + 1; } llayout = new linearlayout[b]; (int j = 0; j < b; j++) { int x = 0; x = x + (j * 5); llayout[j] = new linearlayout(loyaltyprogram.this); llayout[j].setlayoutparams(new layoutparams( layoutparams.wrap_content, layoutparams.wrap_content)); llayout[j].setorientation(linearlayout.vertical); llmain.addview(llayout[j]); (int = x; < x + 5; i++) { if (x > a) { break; } else { if (testarraylist.contains(idsplit[i])) { cb = new checkbox(loyaltyprogram.this); cb.settext(namesplit[i] + (i + 1)); cb.setid(i + 1); cb.setchecked(true); cb.settextcolor(color.black); cb.settextsize(12f); cb.setbuttondrawable(r.drawable.checkbox); cb.setpadding(35, 5, 25, 5); if (count1.equals(1)) { cb.setenabled(true); } else { cb.setenabled(false); } llayout[j].addview(cb); } else { cb = new checkbox(loyaltyprogram.this); cb.settext(namesplit[i] + (i + 1)); cb.setid(i + 1); cb.settextcolor(color.black); cb.settextsize(12f); cb.setbuttondrawable(r.drawable.checkbox); cb.setpadding(35, 5, 25, 5); if (count1.equals(1)) { cb.setenabled(true); } else { cb.setenabled(false); } llayout[j].addview(cb); } } } } } else { toast.maketext(getapplicationcontext(), "data empty", toast.length_long).show(); mpv.setenabled(true); discount.setenabled(true); } pdialog.dismiss(); } } }

just within loop , if , else condition

cb.settag(i+99); // set tag values can refer them later. cb.setoncheckedchangelistener(handlecheck(cb)); // here pass checkbox object.

then handlecheck method

private oncheckedchangelistener handlecheck (final checkbox chk) { homecoming new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if(!ischecked){ toast.maketext(getapplicationcontext(), "you unchecked " + chk.gettag(), toast.length_long).show(); } else { toast.maketext(getapplicationcontext(), "you checked " + chk.gettag(), toast.length_long).show(); } } }; }

hope gives idea. happy coding :)

java android checkbox android-checkbox

No comments:

Post a Comment