android - How to get the id of a particular switch in a custom list view and check it -
this getview() code. want access particular switch in custom listview. doing wrong ?
public view getview(final int position, view convertview, viewgroup parent) { if(convertview==null) { //inflate custom layout convertview = inflater.from(parent.getcontext()).inflate(r.layout.list_item,parent,false); viewholder=new viewholder(); //cache views viewholder.sw=(switch) convertview.findviewbyid(r.id.switch1); viewholder.name=(textview) convertview.findviewbyid(r.id.name); viewholder.rollno=(textview) convertview.findviewbyid(r.id.roll); viewholder.id=(textview) convertview.findviewbyid(r.id.id); //link cached views convertview convertview.settag(viewholder); } else viewholder=(viewholder) convertview.gettag(); //set data displayed viewholder.name.settext(studentlist.get(position).get("name").tostring()); viewholder.rollno.settext(studentlist.get(position).get("roll_no").tostring()); viewholder.id.settext(studentlist.get(position).get("id").tostring()); viewholder.sw.settext(""); (int s : absentmarked) { log.d("marked",integer.tostring(s)); if(s==1){ log.d("entered","yes"); viewholder.sw=(switch) convertview.findviewbyid(r.id.switch1); viewholder.sw.setchecked(true); } }
this problem exist. checks switches true. want access particular switch in list view , check it. absentmarked array list contains 0 , 1. want check switch true corresponding 1 value
for (int s : absentmarked) { log.d("marked",integer.tostring(s)); if(s==1){ log.d("entered","yes"); viewholder.sw=(switch) convertview.findviewbyid(r.id.switch1); viewholder.sw.setchecked(true); } }
you should add field absentmarked in model.
if(studentlist.get(position).get("absentmarked")==1){ log.d("entered","yes"); viewholder.sw.setchecked(true); }else{ viewholder.sw.setchecked(false); }
and add setoncheckedchangelistner switch toggle value of absentmarked follows:
sw.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton compoundbutton, boolean b) { if(b){ setabsentmarked(1); } else{ setabsentmarked(0); } });