listview - Android: Removing a List Item on CheckBox-Click checks next Item -
i have weird behavior. have to-do-list-like function remove listview item when checkbox clicked. however, causes next checkbox checked instantly. idea?
@override public void oncheckboxclick(int position) { mtasklist.remove(position); adapter.notifydatasetchanged(); }
it click gets carried on next item, because item removed fast.
edit:
this customadapters getview() method. rpg themed task list.
@nonnull @override public view getview(final int position, @nullable view convertview, @nonnull viewgroup parent) { view taskitemview = convertview; if (taskitemview == null) { taskitemview = layoutinflater.from(getcontext()).inflate(r.layout.task_item, parent, false); } taskitem currenttaskitem = getitem(position); //set item texts accordingly textview taskname = (textview) taskitemview.findviewbyid(r.id.task_name_xml); taskname.settext(currenttaskitem.getname()); textview difficultytext = (textview) taskitemview.findviewbyid(r.id.task_difficulty_xml); difficultytext.settext(currenttaskitem.getdifficultytext()); textview expgaintext = (textview) taskitemview.findviewbyid(r.id.exp_win_xml); expgaintext.settext("" + currenttaskitem.getexpamountgained() + " exp"); textview goldgaintext = (textview) taskitemview.findviewbyid(r.id.currency_win_xml); goldgaintext.settext("" + currenttaskitem.getgoldamountgained()); checkbox checkbox = (checkbox) taskitemview.findviewbyid(r.id.task_checkbox_xml); checkbox.setchecked(false); //set checkbox listener checkbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mlistener.oncheckboxclick(position); } }); return taskitemview; }
there few things need change.
- always use recycled view pattern listviews. faster , lot more memory efficient
add view holder class task:
class taskviewholder { textview taskname; textview difficultytext; taskviewholder(view root) { taskname = (textview) taskitemview.findviewbyid(r.id.task_name_xml); difficultytext = (textview) taskitemview.findviewbyid(r.id.task_difficulty_xml); } }
then change getview() this:
taskviewholder holder; if(convertview == null) { convertview = layoutinflater.from(getcontext()).inflate(r.layout.task_item, parent, false); holder = new taskviewholder(convertview); convertview.settag(holder); } else holder = (taskviewholder)convertview.gettag(); taskitem currenttaskitem = getitem(position); holder.taskname.settext(currenttaskitem.getdifficultytext()); holder.difficultytext.settext(currenttaskitem.getdifficultytext()); return convertview;
i have included 2 of views idea.
when set oncheckchanged listener set this:
holder.checkbox.settag(position); holder.setonclicklistener(this);
then inside onclick(view v) callback this:
//check if correct view int position = (int)v.gettag(); taskitem t = getitem(position); //do here
again, haven't included views in holder, should add them. should fix issue.
Comments
Post a Comment