android - Is View regenerated as ListView scrolls? -


i have custom baseadapter binding listview. loading list of objects custom class.

i altering layout of of rows, when data changes, create headers (yeah know there logic still have fix works).

my problem is, when scroll listview past orginally-visible, application crashes classcastexception error on headerholder (which see if set breakpoint in catch handler). thinking due view being destroyed , recreated, not sure. can confirm this?

public class mycustombaseadapter extends baseadapter {  private static arraylist<appointment> searcharraylist;   private layoutinflater minflater;   public mycustombaseadapter(context context, arraylist<appointment> results) {   searcharraylist = results;   minflater = layoutinflater.from(context);  }   public int getcount() {   return searcharraylist.size();  }   public object getitem(int position) {   return searcharraylist.get(position);  }   public long getitemid(int position) {   return position;  }   public string lastdate = null;  public string thisitemdate = null;   public view getview(int position, view convertview, viewgroup parent)   {      try      {          thisitemdate = searcharraylist.get(position).getdtstart().tostring().substring(0,10);          if(!thisitemdate.equals(lastdate))          {              headerholder holder;              if (convertview == null)               {                  convertview = minflater.inflate(r.layout.calendar_item_header, null);                   holder = new headerholder();                   holder.txtheader = (textview) convertview.findviewbyid(r.id.header);                   convertview.settag(holder);                  convertview.setpadding(2,2,2,2);              }               else               {                  holder = (headerholder) convertview.gettag();              }               holder.txtheader.settext(thisitemdate);               lastdate = thisitemdate;              return convertview;                           }      }      catch (exception e)      {  //catch exception if         system.err.println("error: " + e.getmessage());      }        viewholder holder;      if (convertview == null)       {          convertview = minflater.inflate(r.layout.calendar_item, null);           holder = new viewholder();          holder.txtattendee = (textview) convertview.findviewbyid(r.id.attendee);          holder.txtsummary = (textview) convertview.findviewbyid(r.id.summary);          holder.txtstarts = (textview) convertview.findviewbyid(r.id.starts);          holder.txtends = (textview) convertview.findviewbyid(r.id.ends);           convertview.settag(holder);          convertview.setpadding(4, 4, 4, 16);      }       else       {          holder = (viewholder) convertview.gettag();      }       holder.txtattendee.settext(searcharraylist.get(position).getattendee());      holder.txtsummary.settext(">" + searcharraylist.get(position).getsummary());      string st = searcharraylist.get(position).getdtstart().tostring();      string en = searcharraylist.get(position).getdtend().tostring();       holder.txtstarts.settext(st.substring(0,16));      holder.txtends.settext(en.substring(0,16));       return convertview;           }   static class viewholder   {      textview txtattendee;      textview txtsummary;      textview txtstarts;      textview txtends;  }   static class headerholder  {      textview txtheader; 

you must tell listview through adapter amount of view types internally creates / updates , view type find @ list position. done through methods:

edit: implementation bit complicated. should redesign adapter explicitly include section titles data items of internal list data structure.

one way doing adapter arraylist<object> instead of arraylist<appointment>. way can have both appointment objects and section title strings in same list.

both methods getview , getitemviewtype need fetch arraylist item @ requested position , check item object type:

public int getitemviewtype(int position) {     object item = getitem(position);      if(item instanceof appointment) {         return 0;     } else {         // it's section title:         return 1;     } } 

you proceed in getview method:

public view getview(int position, view convertview, viewgroup parent) {     object item = getitem(position);      if(convertview == null) {         // create item view first time          if(item instanceof appointment) {             convertview = ... // inflate appointment list view item layout         } else {             convertview = ... // inflate title section list view item layout         }     }      // update list view item view according type:     if(item instanceof appointment) {         appointment = (appointment) item;         // retrieve textviews etc convertview, cache tag in viewholder         // , update these views based on appointment     } else {         // item section header string:         string label = (string) item;         // retrieve label textview convertview... etc...     }      return convertview; } 

there nothing more it.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -