listview - Check boxes goes to checked automatically in Android -
i have list view has several rows created inflator, each row has 2 textview , 3 check boxes. work ok, when uses finger scroll down or noted check box goes checked automatically. don't know why when using scroll.
my code :
public class interactivearrayadapter extends arrayadapter<student> implements filterable { final string namespace = "http://ws.sams.com"; string url = "http://88.198.82.92:8080/sams1/services/insertdataws?wsdl"; // usint // localhost final string method_name = "insertapsentdata"; final string soap_action = "http://ws.sams.com/insertapsentdata"; list<integer> checkboxstate = new arraylist<integer>(200); // list<integer> checkboxstate2 = new arraylist<integer>(200); // list<integer> checkboxstate3 = new arraylist<integer>(200); // set<integer> numbers = new hashset<integer>(); private final list<student> studentnameslist; // student names . private final list<student> studentidslist; // student ids . private final activity context; public interactivearrayadapter(activity context, list<student> studentnameslist, list<student> studentidslist) { super(context, r.layout.activity_list, studentnameslist); this.context = context; this.studentnameslist = studentnameslist; this.studentidslist = studentidslist; } protected button sumbit; class viewholder { protected textview studentname; protected textview studentid; protected checkbox checkbox; protected checkbox checkbox2; protected checkbox checkbox3; protected tablerow row; } public view getview(final int position, view convertview, viewgroup parent) { view view = null; if (convertview == null) { layoutinflater inflator = context.getlayoutinflater(); view = inflator.inflate(r.layout.rep, null); viewholder viewholder = new viewholder(); viewholder.studentname = (textview) view .findviewbyid(r.id.textview07); viewholder.studentid = (textview) view .findviewbyid(r.id.textview08); viewholder.checkbox = (checkbox) view.findviewbyid(r.id.checkbox05); viewholder.checkbox2 = (checkbox) view .findviewbyid(r.id.checkbox02); viewholder.checkbox3 = (checkbox) view .findviewbyid(r.id.checkbox01); viewholder.row = (tablerow) view.findviewbyid(r.id.tablerow05); // final edittext searchedit = (edittext) // view.findviewbyid(r.id.searchlable); typeface font = typeface.createfromasset(context.getassets(), "extrafine.ttf"); typeface fontar = typeface.createfromasset(context.getassets(), "extrafinear.ttf"); viewholder.studentname.settypeface(fontar); viewholder.studentid.settypeface(font); sumbit = (button) context.findviewbyid(r.id.sumbitbtn); view.settag(viewholder); viewholder.row.settag(studentnameslist.get(position)); // here note viewholder.checkbox.settag(studentnameslist.get(position)); viewholder.checkbox2.settag(studentnameslist.get(position)); viewholder.checkbox3.settag(studentnameslist.get(position)); } else { view = convertview; ((viewholder) view.gettag()).checkbox.settag(studentnameslist .get(position)); ((viewholder) view.gettag()).checkbox2.settag(studentnameslist .get(position)); ((viewholder) view.gettag()).checkbox3.settag(studentnameslist .get(position)); ((viewholder) view.gettag()).row.settag(studentnameslist .get(position)); } final viewholder holder = (viewholder) view.gettag(); holder.studentname.settext(studentnameslist.get(position).getname()); holder.studentid.settext(studentidslist.get(position).getname()); holder.checkbox.setchecked(studentnameslist.get(position).isselected()); holder.checkbox2 .setchecked(studentnameslist.get(position).isselected()); holder.checkbox3 .setchecked(studentnameslist.get(position).isselected()); holder.checkbox .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { holder.checkbox2.setchecked(false); holder.checkbox3.setchecked(false); holder.row.setbackgroundresource(r.drawable.green); } else { } ((student) holder.checkbox.gettag()).setselected(true); if (((checkbox) holder.checkbox).ischecked()) { viewgroup row1 = (viewgroup) holder.checkbox .getparent(); view view1 = ((viewgroup) row1).getchildat(4); if (view1 instanceof textview) { holder.studentname = (textview) view1; checkboxstate.add(integer .parseint(holder.studentname.gettext() .tostring())); } } } }); holder.checkbox3 .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { holder.row.setbackgroundresource(r.drawable.red); holder.checkbox2.setchecked(false); holder.checkbox.setchecked(false); } else { } ((student) holder.checkbox3.gettag()).setselected(true); // toast.maketext(context, // holder.text.gettext().tostring() , // toast.length_short).show(); if (((checkbox) holder.checkbox3).ischecked()) { viewgroup row1 = (viewgroup) holder.checkbox3 .getparent(); // (int itempos = 1; itempos < ((viewgroup) // row1).getchildcount(); itempos++) { view view1 = ((viewgroup) row1).getchildat(4); if (view1 instanceof textview) { holder.studentname = (textview) view1; // numbers.add(integer.parseint(holder.studentname.gettext().tostring()));} checkboxstate.add(integer .parseint(holder.studentname.gettext() .tostring())); } // else{ // checkboxstate.add(1);} } } }); holder.checkbox2 .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { holder.row.setbackgroundresource(r.drawable.blue); holder.checkbox.setchecked(false); holder.checkbox3.setchecked(false); } else { } ((student) holder.checkbox2.gettag()).setselected(true); // toast.maketext(context, // holder.text.gettext().tostring() , // toast.length_short).show(); if (((checkbox) holder.checkbox2).ischecked()) { viewgroup row1 = (viewgroup) holder.checkbox2 .getparent(); // (int itempos = 1; itempos < ((viewgroup) // row1).getchildcount(); itempos++) { view view1 = ((viewgroup) row1).getchildat(4); if (view1 instanceof textview) { holder.studentname = (textview) view1; // numbers.add(integer.parseint(holder.studentname.gettext().tostring()));} checkboxstate.add(integer .parseint(holder.studentname.gettext() .tostring())); } // else{ // checkboxstate.add(1);} } } }); sumbit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { (int = 0; < checkboxstate.size(); i++) { toast.maketext(context, checkboxstate.get(i).tostring(), toast.length_short).show(); // webservice propose try { soapobject request = new soapobject(namespace, method_name); propertyinfo pi = new propertyinfo(); pi.setname("sid"); pi.setvalue(integer.parseint(checkboxstate.get(i) .tostring())); pi.settype(integer.class); sharedpreferences mprefs = context .getsharedpreferences("iteacher", 0); // int str1 = mprefs.getint("1",0); int str1 = 1; propertyinfo pi2 = new propertyinfo(); pi2.setname("tid"); pi2.setvalue(str1); pi2.settype(integer.class); propertyinfo pi3 = new propertyinfo(); pi3.setname("class_id"); pi3.setvalue(str1); pi3.settype(integer.class); request.addproperty(pi); request.addproperty(pi2); request.addproperty(pi3); final soapserializationenvelope envelope = new soapserializationenvelope( soapenvelope.ver11); envelope.setoutputsoapobject(request); final httptransportse androidhttptransport = new httptransportse( url); androidhttptransport.call(soap_action, envelope); } catch (exception e) { } } } }); return view; }
}
if know items checked or unchecked before starting set adapter using this
if (checkboxstate.contains(categories.get(position).categoryid)) { holder.checkbox.setchecked(true); } else { holder.checkbox.setchecked(false); }
then continue code this
holder.checkbox .setoncheckedchangelistener(new oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { checkboxstate.add(categories.get(position).categoryid); } else { checkboxstate.remove(categories.get(position).categoryid); } } });
Comments
Post a Comment