Download Images into ListView in android -
i want load list of images listview internet. each list item contains textview (i.e url of image) , imageview. here adapter class code download images , bind listview. images getting mismatched urls. few rows having same image (but different urls). , rows changing image after while. please me out.
public class customadapter extends baseadapter{ private activity act; private string imageurlsarray[]; private layoutinflater inflater; public customadapter(activity activity, string imageurls[]) { this.act = activity; this.imageurlsarray = imageurls; inflater = (layoutinflater)this.act.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return imageurlsarray.length; } @override public object getitem(int arg0) { return null; } @override public long getitemid(int arg0) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; view rowview = convertview; if (rowview == null) { rowview = inflater.inflate(r.layout.list_row, parent, false); holder = new viewholder(); holder.imageview = (imageview)rowview.findviewbyid(r.id.imageviewinlistrow); holder.urltextview = (textview)rowview.findviewbyid(r.id.textviewinlistrow); rowview.settag(holder); }else{ holder = (viewholder) rowview.gettag(); } loadimage(holder.imageview, imageurlsarray[position]); holder.urltextview.settext(imageurlsarray[position]); return rowview; } private void loadimage(imageview iv, string url) { downloadtask asynctask = new downloadtask(); asynctask.execute(iv, url); } class downloadtask extends asynctask<object,object, object>{ private imageview iv; private inputstream = null; private drawable imagedrawable = null; @override protected object doinbackground(object... params) { iv = (imageview) params[0]; try { = new defaulthttpclient().execute(new httppost(params[1].tostring())).getentity().getcontent(); imagedrawable = drawable.createfromstream((inputstream)is , "src name"); } catch (exception e) { e.printstacktrace(); } return imagedrawable; } @override protected void onpostexecute(object response) { super.onpostexecute(response); if(response != null){ iv.setimagedrawable((drawable)response); } } } static class viewholder{ public textview urltextview; public imageview imageview; }
}
use hashmap proper mapping textview. , first convert url image bitmap in adapter only.
Comments
Post a Comment