android - How can I get checkboxes working in my listview? -
i've been trying follow tutorials/ posts on s.o on how checkbox
working in listview
. lots of stuff want first want position of item checked app crashes , nullpointerexception
error.
the error (with more stuff below part) :
java.lang.nullpointerexception @ com.example.chris.tutorialspoint.selectphonecontactadapter.getview(selectphonecontactadapter.java:104)
and line 104 :
convertview.settag(v);
but me looks i've been following tutorials , don't know how adapt these posts problem: getting nullpointerexception in custom listview , crash in listview @ abslistview.obtainview listactivity. can tell me wrong? worked until started experimenting these checkboxes.
here customadapter
code, selectphonecontactadapter
:
public class selectphonecontactadapter extends baseadapter { //define list made out of selectphonecontacts , call thecontactslist public list<selectphonecontact> thecontactslist; //define array list made out of selectcontacts , call arraylist private arraylist<selectphonecontact> arraylist; boolean itemchecked[]; context _c; //define viewholder hold our name , number info, instead of querying // findviewbyid. makes listview run smoother viewholder v; public selectphonecontactadapter(final list<selectphonecontact> selectphonecontacts, context context) { thecontactslist = selectphonecontacts; _c = context; this.arraylist = new arraylist<selectphonecontact>(); this.arraylist.addall(thecontactslist); itemchecked = new boolean[thecontactslist.size()]; } @override public int getcount() { system.out.println("the amount in arraylist :" + arraylist.size()); return arraylist.size(); } @override public object getitem(int i) { return thecontactslist.get(i); } @override public long getitemid(int i) { return i; } @targetapi(build.version_codes.lollipop) static class viewholder { // in each cell in listview show items want have // having viewholder caches our ids, instead of having call , load each 1 again , again textview title, phone; checkbox check; } @override public view getview(final int i, view convertview, viewgroup viewgroup) { //we're naming our convertview view view view = convertview; if (view == null) { v = new viewholder(); system.out.println("getview position :" + i); //if there nothing there (if it's null) inflate view layout layoutinflater li = (layoutinflater) _c.getsystemservice(context.layout_inflater_service); view = li.inflate(r.layout.phone_inflate_listview, null); // so, example, title cast name id, in phone_inflate_listview, // phone cast id called no etc v.title = (textview) view.findviewbyid(r.id.name); v.phone = (textview) view.findviewbyid(r.id.no); v.check = (checkbox) view.findviewbyid(r.id.checkboxcontact); convertview.settag(v); //or else use view (what can see in each row) there } else { view = convertview; } // store holder view final selectphonecontact data = (selectphonecontact) thecontactslist.get(i); //in listview contacts, set name v.title.settext(data.getname()); //in listview contacts, set number v.phone.settext(data.getphone()); v.check.setchecked(false); v.check.setchecked(itemchecked[i]); v.check .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { itemchecked[i] = ischecked; } }); // return completed view render on screen return view; } }
my getters , setters, selectphonecontact
:
public class selectphonecontact { string phone; public string getphone() {return phone;} public void setphone(string phone) { this.phone = phone; } string name; public string getname() { return name; } public void setname(string name) { this.name = name; } boolean selected; public boolean isselected() { return selected; } public void setselected(boolean selected){ this.selected=selected; } }
i can post more code if necessary.
consider code:
@override public view getview(final int i, view convertview, viewgroup viewgroup) { ... view view = convertview; if (view == null) { ... view = li.inflate(r.layout.phone_inflate_listview, null); ... convertview.settag(v); } ... }
first assign value of convertview
view
variable. when null, branch if
statement, assign new value view
via li.inflate()
.
however, reference convertview
later inside if
statement. despite fact wrote view = convertview
above, @ point convertview
still null
.
there 2 ways fix this. first option change convertview.settag(v)
view.settag(v)
. other delete line:
view view = convertview;
and change place reference view
use convertview
instead. there's no need introduce new view view
variable; can work convertview
directly.
Comments
Post a Comment