android - Getview parameter "convertview" not null on new "position" parameter -
i'm using arrayadapter list of own type of objects (only 1 type) , give user option create more items (thus creating more views items). @ point, getview sent new "position" index non-null "convertview". shows first view in last position. after that, when scrolling views mixed up. i'm assuming means manipulated views in ways shouldn't have don't see where. here code:
@override public view getview(int position, view convertview, viewgroup parent) { view v; previewitemholder holder = null; // initialize view if convertview null if (convertview == null) { v = newview(parent, position); } // populate saved holder else { // use previous item if not null v = convertview; } // populate if holder null (newly inflated view) or // if current view's holder's flag true , requires populating if ((holder == null) || (holder.readpopulateflag())) { bindview(position, v); } return v; } private view newview(viewgroup parent, int position) { // getting view somehow... layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); view inflatedview = inflater.inflate(r.layout.preview_element_set, parent, false); previewitemholder holder = new previewitemholder(); holder.set = (set) msets.get(position); holder.previewelementholders = new arraylist<previewelementholder>(); holder.expandarea = (view) inflatedview.findviewbyid(r.id.expandarea); holder.repetitionslabel = (textview) inflatedview.findviewbyid(r.id.previewrepetitionsinput); holder.endlessinput = (checkbox) inflatedview.findviewbyid(r.id.previewsetendlessinput); holder.namelabel = (textview) inflatedview.findviewbyid(r.id.previewsetnamelabel); holder.commentinput = (edittext) inflatedview.findviewbyid(r.id.previewsetcommentinput); holder.soundinput = (edittext) inflatedview.findviewbyid(r.id.previewsetsoundinput); holder.addelementbutton = (button) inflatedview.findviewbyid(r.id.previewsetaddelements); holder.expand = (view) inflatedview.findviewbyid(r.id.infoarea); holder.collapse = (view) inflatedview.findviewbyid(r.id.collapse); final int setslength = holder.set.getelements().size(); (int = 0; < setslength; i++) { aelement currelement = holder.set.getelements().get(i); // creating new element holder according type if (currelement instanceof rest) { holder.previewelementholders.add(new previewrestholder()); } else if (currelement instanceof timeexercise) { holder.previewelementholders.add(new previewtimeexerciseholder()); } else if (currelement instanceof repetitionexercise) { holder.previewelementholders.add(new previewrepetitionexerciseholder()); } view currlayout = inflateelement(currelement, inflater, i, holder.previewelementholders.get(i)); // add child before hairline, collapse image , add // button // (3 last children of expandarea view ((viewgroup) holder.expandarea).addview(currlayout, ((viewgroup) holder.expandarea).getchildcount() - children_index_after_phases_label); } inflatedview.settag(holder); return inflatedview; } private void bindview(int position, view inflatedview) { final previewitemholder holder = (previewitemholder) inflatedview.gettag(); holder.set.setid(position); holder.endlessinput.setchecked(holder.set.getendless()); holder.soundinput.settext(holder.set.getsound()); holder.namelabel.settext(holder.set.getname()); holder.commentinput.settext(holder.set.getcomment()); // make sure there name. if none, put default if (holder.namelabel.gettext().equals("")) { holder.namelabel.settext(r.string.default_set_name); } // set repetitions value according endless flag if (holder.set.getendless()) { holder.repetitionslabel.settext(r.string.infinity); } else { holder.repetitionslabel.settext(string.valueof(holder.set.getrepetitions())); } // set click listeners holder.endlessinput.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // save endless flag holder.set.setendless(ischecked); // if endless set - dropset if (ischecked) { holder.repetitionslabel.settext(r.string.infinity); } else { // regular set holder.repetitionslabel.settext(string.valueof(holder.set.getrepetitions())); } hideshowrepsweights(holder); } }); holder.repetitionslabel.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { numericdialog instance = numericdialog.newinstance(holder, holder.set, numericdialog.integer_mode, consts.set_repetitions_method_name); instance.show(((activity) getcontext()).getfragmentmanager(), null); } }); holder.namelabel.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // setting flag true allow populating view holder.repopulateflag = true; setnamedialog instance = setnamedialog.newinstance(holder.set); instance.show(((activity) getcontext()).getfragmentmanager(), null); } }); holder.commentinput.setonfocuschangelistener(new onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { // after focus lost, save text set holder.set.setcomment(holder.commentinput.gettext().tostring()); } } }); // todo change dialog allows selection of sounds holder.soundinput.setonfocuschangelistener(new onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { // after focus lost, save text set holder.set.setsound(holder.soundinput.gettext().tostring()); } } }); holder.expand.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // change visibility - show expandarea , data holder.expandarea.setvisibility(view.visible); holder.expand.setvisibility(view.gone); holder.collapse.setvisibility(view.visible); } }); holder.collapse.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // change visibility - hide expandarea , data holder.expandarea.setvisibility(view.gone); holder.collapse.setvisibility(view.gone); holder.expand.setvisibility(view.visible); } }); holder.addelementbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { addelementdialog instance = addelementdialog.newinstance(holder); instance.show(((activity) getcontext()).getfragmentmanager(), null); } }); // populate elements (previewelementholder elementholder : holder.previewelementholders) { populateelement(elementholder, holder); } // hide/show if needed - should put somewere else? hideshowrepsweights(holder); }
please tell me if think should upload more methods make things clearer.
a friend explained problem me , seems work. listview holds small number of views , recycles them time. in case have nexus 4 , seems have 7 views total because 8th 1 started cause trouble. missing in getview() condition checking correlation between position , id of current item within arrayadapter. here how looks works:
@override public view getview(int position, view convertview, viewgroup parent) { view v; previewitemholder holder = null; // initialize view if convertview null if (convertview == null) { v = newview(parent, position); } // populate saved holder else { // if position , id of set not match, view needs re-created, not recycled if (((previewitemholder) convertview.gettag()).set.getid() != position) { v = newview(parent, position); } else { // use previous item if not null v = convertview; // holder holder = (previewitemholder) v.gettag(); } } // populate if holder null (newly inflated view) or // if current view's holder's flag true , requires populating if ((holder == null) || (holder.readpopulateflag())) { bindview(position, v); } return v; }
Comments
Post a Comment