Android ListView onClickListener Custom Adapter -
i read posts custom adapters , how index them seems cannot make mine work. overwrite getview , xml contains 1 textview , 2 buttons. made both buttons detected onclicklistener couldnt differentiate listview element 1 triggered clickevent. tried different approach nullpointerexception in onclick method.
@override public view getview(int position, view convertview, viewgroup parent){ viewholder holder; if(convertview == null){ layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.listexample, null); holder = new viewholder(); holder.textview = (textview) convertview.findviewbyid(r.id.commandline_text); holder.start = (button) convertview.findviewbyid(r.id.test_start_button); holder.stop = (button) convertview.findviewbyid(r.id.test_stop_button); convertview.settag(holder); convertview.findviewbyid(r.id.commandline_text); convertview.findviewbyid(r.id.test_start_button); convertview.findviewbyid(r.id.test_stop_button); } else { holder = (viewholder) convertview.gettag(); } holder.textview.settext(this.getitem(position)); holder.start.setonclicklistener(this); holder.stop.setonclicklistener(this); return convertview; } @override public void onclick(view v) { //here want know button of 2 (start,stop) clicked , position int position =(integer)v.gettag(); log.d("onclick","position: "+position); } static class viewholder { textview textview; button start; button stop; }
try using getpositionforview(v)
find position correlates button pressed.
Comments
Post a Comment