android - Remove Dynamically added layouts from listview extends base adapter -
here have number of layouts inflated, when length of ei.duedate.size()
0 views not removed.
i have search lots of didn't how can remove views listview
.
i have inflated 5-6 child layouts when size goes 1-2 inflated views can not remove.
please me if guys have idea.
below you'll find adapter
code:
public class immunisationadapter extends baseadapter { @override public int getcount() { // todo auto-generated method stub return immumainarraylist.size(); } @override public object getitem(int position) { // todo auto-generated method stub return immumainarraylist.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(final int position, view view, viewgroup parent) { // todo auto-generated method stub viewholder holder = null; final immunimodel ei = immumainarraylist.get(position); if (view == null) { holder = new viewholder(); view = layoutinflater.from(getactivity()).inflate(r.layout.raw_main_immunisation, parent, false); holder.layout = (viewgroup) view.findviewbyid(r.id.ll); holder.imageview = (imageview) view.findviewbyid(r.id.optin); holder.txt_count = (textviewplus)view.findviewbyid(r.id.txt_circle); holder.mg_type = (imageview) view.findviewbyid(r.id.mg_type); holder.txt_main = (textviewplus) view.findviewbyid(r.id.txt_main); holder.txt_new_elder = (textviewplus) view.findviewbyid(r.id.txt_new_elder); log.e("size of childimmunisation",ei.duedate.size()+""); (int = 0; < ei.duedate.size(); i++) { view chiledview = layoutinflater.from(getactivity()).inflate(r.layout.raw_child_immunisation, null); viewgroup.layoutparams layoutparams = new viewgroup.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); checkbox jabchk = (checkbox) chiledview.findviewbyid(r.id.chk); textviewplus txt_date = (textviewplus) chiledview.findviewbyid(r.id.txt_date); textviewplus txt_lbl = (textviewplus) chiledview.findviewbyid(r.id.text_admin); jabchk.settext("jab " + (i+1)); chiledview.settag(i); chiledview.setlayoutparams(layoutparams); txt_date.settext(ei.duedate.get(i).due_on); if (ei.duedate.get(i).status.equals("administered")) { jabchk.setchecked(true); txt_lbl.settext("administered on "); }else if (ei.duedate.get(i).status.equals("planned")) { jabchk.setchecked(false); txt_lbl.settext("estimated due date "); } holder.layout.addview(chiledview, i); chiledview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub int pos = (integer) v.gettag(); intent intent = new intent(getactivity(), editjabimmunisationactivity.class); intent.putextra("pos", (pos+1) + ""); intent.putextra("status", ei.duedate.get(pos).status); editor.putstring(utils.jabname, ei.name); editor.putstring(utils.jabid, ei.duedate.get(pos).id); editor.putstring(utils.jabdate, ei.duedate.get(pos).due_on); editor.commit(); intent.putextra("location", ei.duedate.get(pos).jab_location); intent.putextra("type", ei.duedate.get(pos).jab_type); intent.putextra("note", ei.duedate.get(pos).note); intent.putextra("doc", ei.duedate.get(pos).doc_name); intent.putextra("facility", ei.duedate.get(pos).facility); startactivity(intent); getactivity().overridependingtransition(r.anim.slide_in_right, r.anim.slide_out_left); } }); } view.settag(holder); } else { holder = (viewholder) view.gettag(); } ((viewgroup)holder.layout.getparent()).removeview(holder.layout); holder.txt_main.settext(immumainarraylist.get(position).name); if (immumainarraylist.get(position).category.equals("drops")) { holder.mg_type.setimageresource(r.drawable.drop); }else if (immumainarraylist.get(position).category.equals("injection")) { holder.mg_type.setimageresource(r.drawable.syring); }else if (immumainarraylist.get(position).category.contains("pill")) { holder.mg_type.setimageresource(r.drawable.pill); } holder.imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub popupwindowdogs = popupwindow.popupwindowdogs(position); popupwindowdogs.showasdropdown(v, -5, 0); } }); holder.txt_new_elder.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.e("helof", immumainarraylist.get(position).vaccin_id); try { addnewjab(immumainarraylist.get(position).vaccin_id, token); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); return view; } }
you need take view recycling account. holder.layout viewgroup
may have children in it, need removed first.
@override public view getview(final int position, view view, viewgroup parent) { // todo auto-generated method stub viewholder holder = null; final immunimodel ei = immumainarraylist.get(position); if (view == null) { holder = new viewholder(); view = layoutinflater.from(getactivity()).inflate(r.layout.raw_main_immunisation, parent, false); holder.layout = (viewgroup) view.findviewbyid(r.id.ll); holder.imageview = (imageview) view.findviewbyid(r.id.optin); holder.txt_count = (textviewplus)view.findviewbyid(r.id.txt_circle); holder.mg_type = (imageview) view.findviewbyid(r.id.mg_type); holder.txt_main = (textviewplus) view.findviewbyid(r.id.txt_main); holder.txt_new_elder = (textviewplus) view.findviewbyid(r.id.txt_new_elder); view.settag(holder); } else { holder = (viewholder) view.gettag(); holder.layout.removeallviews(); } log.e("size of childimmunisation",ei.duedate.size()+""); (int = 0; < ei.duedate.size(); i++) { view chiledview = layoutinflater.from(getactivity()).inflate(r.layout.raw_child_immunisation, null); viewgroup.layoutparams layoutparams = new viewgroup.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); checkbox jabchk = (checkbox) chiledview.findviewbyid(r.id.chk); textviewplus txt_date = (textviewplus) chiledview.findviewbyid(r.id.txt_date); textviewplus txt_lbl = (textviewplus) chiledview.findviewbyid(r.id.text_admin); jabchk.settext("jab " + (i+1)); chiledview.settag(i); chiledview.setlayoutparams(layoutparams); txt_date.settext(ei.duedate.get(i).due_on); if (ei.duedate.get(i).status.equals("administered")) { jabchk.setchecked(true); txt_lbl.settext("administered on "); } else if (ei.duedate.get(i).status.equals("planned")) { jabchk.setchecked(false); txt_lbl.settext("estimated due date "); } holder.layout.addview(chiledview, i); chiledview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub int pos = (integer) v.gettag(); intent intent = new intent(getactivity(), editjabimmunisationactivity.class); intent.putextra("pos", (pos+1) + ""); intent.putextra("status", ei.duedate.get(pos).status); editor.putstring(utils.jabname, ei.name); editor.putstring(utils.jabid, ei.duedate.get(pos).id); editor.putstring(utils.jabdate, ei.duedate.get(pos).due_on); editor.commit(); intent.putextra("location", ei.duedate.get(pos).jab_location); intent.putextra("type", ei.duedate.get(pos).jab_type); intent.putextra("note", ei.duedate.get(pos).note); intent.putextra("doc", ei.duedate.get(pos).doc_name); intent.putextra("facility", ei.duedate.get(pos).facility); startactivity(intent); getactivity().overridependingtransition(r.anim.slide_in_right, r.anim.slide_out_left); } }); } holder.txt_main.settext(immumainarraylist.get(position).name); if (immumainarraylist.get(position).category.equals("drops")) { holder.mg_type.setimageresource(r.drawable.drop); }else if (immumainarraylist.get(position).category.equals("injection")) { holder.mg_type.setimageresource(r.drawable.syring); }else if (immumainarraylist.get(position).category.contains("pill")) { holder.mg_type.setimageresource(r.drawable.pill); } holder.imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub popupwindowdogs = popupwindow.popupwindowdogs(position); popupwindowdogs.showasdropdown(v, -5, 0); } }); holder.txt_new_elder.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.e("helof", immumainarraylist.get(position).vaccin_id); try { addnewjab(immumainarraylist.get(position).vaccin_id, token); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); return view; }
i showed quick , easy way. better way not call removeallviews()
, you're looping through ei.duedate
, call getchildat(i)
see if raw_child_immunisation
view there , use rather inflating new view. have use removeviews(start, count)
clear vestigial raw_child_immunisation
views @ end of loop.
here code changes strategy:
@override public view getview(final int position, view view, viewgroup parent) { // todo auto-generated method stub viewholder holder = null; final immunimodel ei = immumainarraylist.get(position); if (view == null) { holder = new viewholder(); view = layoutinflater.from(getactivity()).inflate(r.layout.raw_main_immunisation, parent, false); holder.layout = (viewgroup) view.findviewbyid(r.id.ll); holder.imageview = (imageview) view.findviewbyid(r.id.optin); holder.txt_count = (textviewplus)view.findviewbyid(r.id.txt_circle); holder.mg_type = (imageview) view.findviewbyid(r.id.mg_type); holder.txt_main = (textviewplus) view.findviewbyid(r.id.txt_main); holder.txt_new_elder = (textviewplus) view.findviewbyid(r.id.txt_new_elder); view.settag(holder); } else { holder = (viewholder) view.gettag(); int childcount = holder.layout.getchildcount(); if (childcount > ei.duedate.size()) { // remove views holder.layout.removeviews(ei.duedate.size(), childcount - ei.duedate.size()); } } log.e("size of childimmunisation",ei.duedate.size()+""); (int = 0; < ei.duedate.size(); i++) { // if view exists, use view chiledview = holder.layout.getchildat(i); if (chiledview == null) { // otherwise create new view , add view group chiledview = layoutinflater.from(getactivity()).inflate(r.layout.raw_child_immunisation, null); holder.layout.addview(chiledview, i); } viewgroup.layoutparams layoutparams = new viewgroup.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); checkbox jabchk = (checkbox) chiledview.findviewbyid(r.id.chk); textviewplus txt_date = (textviewplus) chiledview.findviewbyid(r.id.txt_date); textviewplus txt_lbl = (textviewplus) chiledview.findviewbyid(r.id.text_admin); jabchk.settext("jab " + (i+1)); chiledview.settag(i); chiledview.setlayoutparams(layoutparams); txt_date.settext(ei.duedate.get(i).due_on); if (ei.duedate.get(i).status.equals("administered")) { jabchk.setchecked(true); txt_lbl.settext("administered on "); } else if (ei.duedate.get(i).status.equals("planned")) { jabchk.setchecked(false); txt_lbl.settext("estimated due date "); } chiledview.setonclicklistener(new view.onclicklistener() { . . . (rest of code same)
Comments
Post a Comment