android - Assigning unique tag to list item in a list -
i need setting tags in viewholder object. have list longer screen (on phone) , need set different tag each view. can see in preview app (link below)in exterior fragment first item , last item binded together. dont' want that, want every views unique. how can that.
https://drive.google.com/file/d/0b2xlu23gjnfvtegtbkprothtmmm/edit?usp=sharing
thanks
my inspectionlistadapter
public class inspectionlistadapter extends arrayadapter<string> { private final activity context; private final string[] inspectiontitles; private final string[] description; public inspectionlistadapter(activity context, string[] inspectiontitles, string[] description) { super(context, r.layout.inspection_item_layout, inspectiontitles); this.context = context; this.inspectiontitles = inspectiontitles; this.description = description; } public static class viewholder { public imageview imageview, expandbutton; public textview txttitle; public textview txtdescription; public relativelayout expandlayout; public edittext owndesc, estcost; public layerdrawable listitem; public gradientdrawable color; public checkbox checkrepair, checkdone; public relativelayout mainlayout; } @override public view getview(int position, view view, viewgroup parent) { final viewholder viewholder; view rowview = view; if (rowview == null) { layoutinflater inflater = context.getlayoutinflater(); rowview = inflater.inflate(r.layout.inspection_item_layout, null, true); viewholder = new viewholder(); viewholder.mainlayout = (relativelayout) rowview .findviewbyid(r.id.item_layout_main); viewholder.txttitle = (textview) rowview .findviewbyid(r.id.textviewitemtitle); viewholder.txtdescription = (textview) rowview .findviewbyid(r.id.textviewtip); viewholder.listitem = (layerdrawable) rowview.getresources() .getdrawable(r.drawable.layer_car_background); viewholder.color = (gradientdrawable) (viewholder.listitem .finddrawablebylayerid(r.id.item_list_background)); viewholder.imageview = (imageview) rowview .findviewbyid(r.id.imageviewitem); viewholder.expandbutton = (imageview) rowview .findviewbyid(r.id.imageviewtitle); viewholder.owndesc = (edittext) rowview .findviewbyid(r.id.edittextdescription); viewholder.estcost = (edittext) rowview .findviewbyid(r.id.edittextestcost); viewholder.checkrepair = (checkbox) rowview .findviewbyid(r.id.checkbox2); viewholder.checkdone = (checkbox) rowview .findviewbyid(r.id.checkbox1); viewholder.checkrepair .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean arg1) { // todo auto-generated method stub if (view.ischecked()) { viewholder.checkdone.setchecked(false); } } }); viewholder.checkdone .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean arg1) { // todo auto-generated method stub if (view.ischecked()) { viewholder.checkrepair.setchecked(false); } } }); viewholder.expandlayout = (relativelayout) rowview .findviewbyid(r.id.relativelayoutexpandable); viewholder.expandlayout.setvisibility(view.gone); viewholder.expandbutton .setonclicklistener(new onclicklistener() { public void onclick(view v) { // todo auto-generated method stub if (viewholder.expandlayout.getvisibility() == view.visible) { viewholder.expandbutton .setimageresource(r.drawable.ic_action_expand); viewholder.expandlayout .setvisibility(view.gone); if (viewholder.checkdone.ischecked()) { viewholder.mainlayout .setbackgroundresource(r.drawable.layer_car_background_green); } if (viewholder.checkrepair.ischecked()) { viewholder.mainlayout .setbackgroundresource(r.drawable.layer_car_background_red); } } else { viewholder.expandbutton .setimageresource(r.drawable.ic_action_collapse); viewholder.expandlayout .setvisibility(view.visible); viewholder.mainlayout .setbackgroundresource(r.drawable.layer_car_background); } } }); rowview.settag(viewholder); } else { viewholder = (viewholder) rowview.gettag(); } viewholder.txttitle.settext(inspectiontitles[position]); viewholder.txtdescription.settext("tip: \n" + description[position]); viewholder.imageview.setimageresource(r.drawable.ic_launcher); viewholder.expandbutton .setimageresource(r.drawable.ic_action_expand); return rowview; }
}
the mistake in onclick of onclicklistener viewholder.expandbutton. there start immedeately 'if (viewholder.....)`. wrong viewholder @ moment user clicks view many viewholders may have been created , died. have obtain right viewholder first. (untested)
view rowview = (view)v.getparent(); viewholder viewholder =(viewholder) rowview.gettag();
you put in onclick() method. final viewholder viewholder;
made final in order use in onclick() , oncheckedchanged. remove final. unfortunately getparent() not work in oncheckedchanged. different.
viewholder.checkdone.settag(viewholder.checkrepair); viewholder.checkdone .setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean arg1) { checkbox checkbox = (checkbox)view.gettag(); if (view.ischecked()) { //viewholder.checkrepair.setchecked(false); checkbox.setchecked(false); } } });
do same other checkbox.
now color expands on view. reason should set default color @ same time set new txttitle, txtdescription , on. add there viewholder.mainlayout.setbackgroundresource(r.drawable.layer_car_background);
.
for rest code misses keeping state. if item expanded , user scrolls list , down (i added more items able scroll more) expanded state vanishes. checkboxes don't stay checked , color vanishes (add more items see). should add boolean arrrays (at least three) in keep state. update arrays in events. in getview() read arrays , put colors , checkboxes checked accordingly. not work add arrays see...
Comments
Post a Comment