android - How to get rid of lag/jitter in listview? Can anybody help me with my code on how to cache it? -


i trying understand concept of cache. can me code, listview having lag whenever scroll or down. tried understanding android tutorial on this. didnt me much.

as told people helped me changed view viewholder.

but coming accross new error. return type incompatible arrayadapter.getview(int, view, viewgroup)

public class movieratingsactivity extends listactivity

{

private arraylist<movie> movies = new arraylist<movie>();   private layoutinflater minflater; private lrucache<string, bitmap> cache; /** called when activity first created. */ public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     initializeui(); }  private void initializeui() {     minflater = (layoutinflater) this.getsystemservice(context.layout_inflater_service);             inputstream inputstream = getresources().openrawresource(   r.raw.ratings);     movies = movie.loadfromfile(inputstream);            setlistadapter(new rowiconadapter(this, r.layout.listrow, r.id.row_label, movies)); }  /** custom row adatper -- displays icon next movie name */ class rowiconadapter extends arrayadapter<movie>  {     private arraylist<movie> movies;             public rowiconadapter(context c, int rowresourceid, int textviewresourceid,              arraylist<movie> items)     {         super(c, rowresourceid, textviewresourceid, items);         movies  = items;     }     /*     public view getview(int pos, view convertview, viewgroup parent)     {         view row = minflater.inflate(r.layout.listrow, parent, false);         movie currmovie = movies.get(pos);           if (currmovie != null)         {             imageview icon = (imageview) row.findviewbyid(r.id.row_icon);             textview movietext = (textview) row.findviewbyid(r.id.row_label);             textview votestext = (textview) row.findviewbyid(r.id.row_subtext);             movietext.settext(currmovie.getname());             string votesstr = currmovie.getvotes()+" votes";             votestext.settext(votesstr);             bitmap movieicon = getmovieicon(currmovie.getname(), currmovie.getrating());             icon.setimagebitmap(movieicon);             log.w("mvmvmvmvmvmv", "creating row view @ position "+pos+" movie "+currmovie.getname());         }           return row;              } } 

*/ /new code error thrown/

public viewholder getview(int pos, view convertview, viewgroup parent)

{

        viewholder holder = new viewholder();            movie currmovie = movies.get(pos);           if (currmovie != null)         {             holder.icon = (imageview) convertview.findviewbyid(r.id.row_icon);             holder.movietext = (textview) convertview.findviewbyid(r.id.row_label);             holder.votestext = (textview) convertview.findviewbyid(r.id.row_subtext);             holder.movietext.settext(currmovie.getname());             string votesstr = currmovie.getvotes()+" votes";             holder.votestext.settext(votesstr);             bitmap movieicon = getmovieicon(currmovie.getname(), currmovie.getrating());             holder.icon.setimagebitmap(movieicon);             log.w("mvmvmvmvmvmv", "creating row view @ position "+pos+" movie "+currmovie.getname());         }               return holder;     } }  /** creates unique movie icon based on name , rating */ private bitmap getmovieicon(string moviename, string movierating) {     int bgcolor = getcolor(moviename);     bitmap b = bitmap.createbitmap(48, 48, bitmap.config.argb_8888);     b.erasecolor(bgcolor); // fill bitmap color     canvas c = new canvas(b);     paint p = new paint();     p.setantialias(true);     p.setcolor(gettextcolor(bgcolor));     p.settextsize(24.0f);     c.drawtext(movierating, 8, 32, p);     return b; }  /** construct color movie name */ private int getcolor(string name) {     string hex = tohexstring(name);     string red = "#"+hex.substring(0,2);     string green = "#"+hex.substring(2,4);     string blue = "#"+hex.substring(4,6);     string alpha = "#"+hex.substring(6,8);     int color = color.argb(integer.decode(alpha), integer.decode(red),                              integer.decode(green), integer.decode(blue));     return color; }  /** given movie name -- generate hex value hashcode */ private string tohexstring(string name) {     int hc = name.hashcode();     string hex = integer.tohexstring(hc);     if (hex.length() < 8)     {         hex = hex+hex+hex;         hex = hex.substring(0,8); // use default color value     }     return hex; }  /** crude optimization obtain contrasting color -- not work yet */ private int gettextcolor(int bg) {      int r = color.red(bg);     int g = color.green(bg);     int b = color.blue(bg);     string hex = integer.tohexstring(r)+integer.tohexstring(g);     hex += integer.tohexstring(b);      int cdec = integer.decode("#"+hex);     if (cdec > 0xffffff/2)  // go dark lighter shades         return color.rgb(0, 0, 0);     else     {         r = (r+128)%256;         g = (g+128)%256;         b = (b+128)%256;         return color.rgb(r,g,b);     } } 

}

there few things improve performance.

  1. look using viewholder pattern prevent findviewbyid() calls
  2. by implementing viewholder pattern remove unecessary inflations of layout
  3. creating paint object expensive instantiate 1 oncreate , reuse updating properties change.

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -