lazylist - Android ListView ArrayAdapter -
i'm trying use lazylist load images , i'm using array datas. cant figure out how implement view holder. please check codes below. line of code
imageloader.displayimage(worldpopulationlist.get(position).getflag(), holder.flag);
the method displayimage(string, imageview) in type imageloader not applicable arguments (int, imageview)
listviewadapter
public class listviewadapter extends baseadapter { // declare variables context mcontext; layoutinflater inflater; private list<worldpopulation> worldpopulationlist = null; private arraylist<worldpopulation> arraylist; imageloader imageloader; public listviewadapter(context context, list<worldpopulation> worldpopulationlist) { mcontext = context; this.worldpopulationlist = worldpopulationlist; inflater = layoutinflater.from(mcontext); this.arraylist = new arraylist<worldpopulation>(); this.arraylist.addall(worldpopulationlist); imageloader = new imageloader(mcontext); } public class viewholder { textview rank; textview country; textview population; imageview flag; } @override public int getcount() { return worldpopulationlist.size(); } @override public worldpopulation getitem(int position) { return worldpopulationlist.get(position); } @override public long getitemid(int position) { return position; } public view getview(final int position, view view, viewgroup parent) { final viewholder holder; if (view == null) { holder = new viewholder(); view = inflater.inflate(r.layout.listview_item, null); holder.rank = (textview) view.findviewbyid(r.id.rank); holder.country = (textview) view.findviewbyid(r.id.country); holder.population = (textview) view.findviewbyid(r.id.population); holder.flag = (imageview) view.findviewbyid(r.id.flag); view.settag(holder); } else { holder = (viewholder) view.gettag(); } holder.rank.settext(worldpopulationlist.get(position).getrank()); holder.country.settext(worldpopulationlist.get(position).getcountry()); holder.population.settext(worldpopulationlist.get(position).getpopulation()); //holder.flag.setimageresource(worldpopulationlist.get(position).getflag()); imageloader.displayimage(worldpopulationlist.get(position).getflag(), holder.flag);
imageloader
public class imageloader { memorycache memorycache = new memorycache(); private map<imageview, string> imageviews = collections .synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; // handler display images in ui thread handler handler = new handler(); public imageloader(context context) { executorservice = executors.newfixedthreadpool(5); } final int tempimage = r.drawable.temp_image; public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap = memorycache.get(url); if (bitmap != null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(tempimage); } } private void queuephoto(string url, imageview imageview) { phototoload p = new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f = new file(url); bitmap b = decodefile(f); if (b != null) { return b; } return null; } // decodes image , scales reduce memory consumption private bitmap decodefile(file f) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; fileinputstream stream1 = new fileinputstream(f); bitmapfactory.decodestream(stream1, null, o); stream1.close(); // find correct scale value. should power of 2. // recommended size 512 final int required_size = 512; int width_tmp = o.outwidth, height_tmp = o.outheight; int scale = 1; while (true) { if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; fileinputstream stream2 = new fileinputstream(f); bitmap bitmap = bitmapfactory.decodestream(stream2, null, o2); stream2.close(); return bitmap; } catch (filenotfoundexception e) { } catch (ioexception e) { e.printstacktrace(); } return null; } // task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i) { url = u; imageview = i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload) { this.phototoload = phototoload; } @override public void run() { try { if (imageviewreused(phototoload)) return; bitmap bmp = getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if (imageviewreused(phototoload)) return; bitmapdisplayer bd = new bitmapdisplayer(bmp, phototoload); handler.post(bd); } catch (throwable th) { th.printstacktrace(); } } } boolean imageviewreused(phototoload phototoload) { string tag = imageviews.get(phototoload.imageview); if (tag == null || !tag.equals(phototoload.url)) return true; return false; } // used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p) { bitmap = b; phototoload = p; } public void run() { if (imageviewreused(phototoload)) return; if (bitmap != null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(tempimage); } } public void clearcache() { memorycache.clear(); } }
worldpopulation
public class worldpopulation { private string rank; private string country; private string population; private int flag; public worldpopulation(string rank, string country, string population, int flag) { this.rank = rank; this.country = country; this.population = population; this.flag = flag; } public string getrank() { return this.rank; } public string getcountry() { return this.country; } public string getpopulation() { return this.population; } public int getflag() { return this.flag; } }
as getting
the method displayimage(string, imageview) in type imageloader not applicable arguments (int, imageview)
means worldpopulationlist.get(position).getflag()
returning integer value instead of image url. make sure using right getter/setter image url.
imageloader.displayimage(string,imageview)
method takes 2 parameter first string url image , second imageview instance want set downloaded image
Comments
Post a Comment