android - Spinner by arrayadapter acts "weird" -
i have spinner. have overridden custom dropdown getview.
i'm working in android 2.2
problem selected value gets replaced "default"(empty field) inserted value when reopening selector: images explain below, , overridden code below them:
now pressing spinner:
now pressing robs, , pressing spinner again
dropdown overridden method:
@override public view getdropdownview(int position, view convertview, viewgroup parent) { view v; viewholder holder; if (convertview == null) { v = view.inflate(ctx, r.layout.fav_spinner_item, null); holder = new viewholder(); holder.indentationfield = (imageview) v .findviewbyid(r.id.indent); holder.namefield = (textview) v.findviewbyid(r.id.name); holder.levelcount = mlist.get(position).gettier() != -1 ? mlist .get(position).gettier() : 0; holder.namefield.settext(mlist.get(position).getdisplayname()); if (holder.levelcount == 0) { holder.indentationfield.setvisibility(view.gone); } else { relativelayout.layoutparams params = (relativelayout.layoutparams) holder.indentationfield.getlayoutparams(); params.setmargins(params.leftmargin+((params.leftmargin+15)*holder.levelcount), params.topmargin, params.rightmargin, params.bottommargin); holder.indentationfield.setlayoutparams(params); } v.settag(holder); } else { v = convertview; holder = (viewholder) convertview.gettag(); } return v; }
you aren't using viewholder
pattern properly. since getdropdownview()
can called any item index , can passed any recycled view
, need reset dynamic data every time. this:
@override public view getdropdownview(int position, view convertview, viewgroup parent) { view v; viewholder holder; if (convertview == null) { v = view.inflate(ctx, r.layout.fav_spinner_item, null); holder = new viewholder(); holder.indentationfield = (imageview) v .findviewbyid(r.id.indent); holder.namefield = (textview) v.findviewbyid(r.id.name); v.settag(holder); } else { v = convertview; holder = (viewholder) convertview.gettag(); } // rest of dynamic data, based on item position holder.levelcount = mlist.get(position).gettier() != -1 ? mlist .get(position).gettier() : 0; holder.namefield.settext(mlist.get(position).getdisplayname()); if (holder.levelcount == 0) { holder.indentationfield.setvisibility(view.gone); } else { relativelayout.layoutparams params = (relativelayout.layoutparams) holder.indentationfield.getlayoutparams(); params.setmargins(params.leftmargin+((params.leftmargin+15)*holder.levelcount), params.topmargin, params.rightmargin, params.bottommargin); holder.indentationfield.setlayoutparams(params); holder.indentationfield.setvisibility(view.visible); } return v; }
also notice line added reset visibility of indentationfield
visible
because might recycling view
visibility set gone
Comments
Post a Comment