Android Custom Listview Image loading without libraries -


i want load images sd card custom listview. listview contains image view , text view. know there many libraries can me achieve this, i want without using such libraries.

problem :-

i loading images async task. calling async task inside getview(). images in last row. is, if there 3 images in folder, image shown in 3rd item of listview.

below code :-

public class myphotoadapter extends baseadapter {      private activity callingactivity;     private arraylist<string> filepaths = new arraylist<string>();//contains file path of images     arraylist<bitmap> myimages = new arraylist<bitmap>();     private int imagewidth;     int position;      imageview iv_photo;     textview tv_address;       public myphotoadapter(activity activity,arraylist<string> paths,int width)     {         this.callingactivity = activity;         this.filepaths = paths;         this.imagewidth = width;     }        @override     public int getcount() {         return filepaths.size();     }      @override     public object getitem(int i) {         return this.filepaths.get(i);     }      @override     public long getitemid(int i) {         return 0;     }      @override     public view getview(int i, view view, viewgroup viewgroup) {          position = i;          layoutinflater inflater = (layoutinflater) callingactivity.getsystemservice(context.layout_inflater_service);         view row = inflater.inflate(r.layout.single_photo,viewgroup, false);          iv_photo = (imageview) row.findviewbyid(r.id.photoview);         tv_address = (textview) row.findviewbyid(r.id.tv_address);          asyncimageloader loader = new asyncimageloader();         loader.execute(filepaths);         return row;     }      class asyncimageloader extends asynctask<arraylist<string>,bitmap,void>{           progressdialog dialog;           @override         protected void onpreexecute() {             super.onpreexecute();             dialog = progressdialog.show(callingactivity,"loading images","please wait ....");         }          @override         protected void doinbackground(arraylist<string>... arrays) {             log.d("test","total images :- "+arrays[0].size());             for(int = 0; i< arrays[0].size(); i++)             {                 bitmap map = decodefile(arrays[0].get(i).tostring(), 150, 150);                 myimages.add(map);                 publishprogress(map);             }              return null;         }          @override         protected void onprogressupdate(bitmap... bitmap) {              iv_photo.setimagebitmap(bitmap[0]);          }          @override         protected void onpostexecute(void avoid) {             //super.onpostexecute(avoid);             dialog.dismiss();             /*for(int = 0; < myimages.size(); ++)             {                 iv_photo.setimagebitmap(myimages.get(i));             }*/         }           public bitmap decodefile(string filepath, int width, int hight) {             try {                  file f = new file(filepath);                  bitmapfactory.options o = new bitmapfactory.options();                 o.injustdecodebounds = true;                 bitmapfactory.decodestream(new fileinputstream(f), null, o);                  final int required_width = width;                 final int required_hight = hight;                 int scale = 1;                 while (o.outwidth / scale / 2 >= required_width                         && o.outheight / scale / 2 >= required_hight)                     scale *= 2;                  bitmapfactory.options o2 = new bitmapfactory.options();                 o2.insamplesize = scale;                 return bitmapfactory.decodestream(new fileinputstream(f), null, o2);             } catch (filenotfoundexception e) {                 e.printstacktrace();             }             return null;         }     } } 

image shown in last list-item. other image views dont show images.

you can follow this know more handling concurrency.

i have modified code ..... please see if helps

public class myphotoadapter extends baseadapter {      private activity callingactivity;     private arraylist<string> filepaths = new arraylist<string>();     arraylist<bitmap> myimages = new arraylist<bitmap>();     private int imagewidth;     int position;      imageview iv_photo;     textview tv_address;       public myphotoadapter(activity activity,arraylist<string> paths,int width)     {         this.callingactivity = activity;         this.filepaths = paths;         this.imagewidth = width;     }        @override     public int getcount() {         return filepaths.size();     }      @override     public object getitem(int i) {         return this.filepaths.get(i);     }      @override     public long getitemid(int i) {         return 0;     }      @override     public view getview(int i, view view, viewgroup viewgroup) {          position = i;          layoutinflater inflater = (layoutinflater) callingactivity.getsystemservice(context.layout_inflater_service);         view row = inflater.inflate(r.layout.single_photo,viewgroup, false);          iv_photo = (imageview) row.findviewbyid(r.id.photoview);         tv_address = (textview) row.findviewbyid(r.id.tv_address);          asyncimageloader loader = new asyncimageloader(iv_photo);         loader.execute(filepaths);         return row;     }      class asyncimageloader extends asynctask<arraylist<string>,bitmap,void>{           progressdialog dialog;         private final weakreference<imageview> imageviewreference;          public asyncimageloader(imageview imageview)         {             imageviewreference = new weakreference<imageview>(imageview);         }          @override         protected void onpreexecute() {             super.onpreexecute();             dialog = progressdialog.show(callingactivity,"loading images","please wait ....");         }          @override         protected void doinbackground(arraylist<string>... arrays) {             log.d("test","total images :- "+arrays[0].size());             for(int = 0; i< arrays[0].size(); i++)             {                 bitmap map = decodefile(arrays[0].get(i).tostring(), 150, 150);                 myimages.add(map);                 publishprogress(map);             }              return null;         }          @override         protected void onprogressupdate(bitmap... bitmap) {              //iv_photo.setimagebitmap(bitmap[0]);              if (imageviewreference != null) {                 imageview imageview = imageviewreference.get();                 if (imageview != null) {                     imageview.setimagebitmap(bitmap[0]);                 }             }          }          @override         protected void onpostexecute(void avoid) {             //super.onpostexecute(avoid);             dialog.dismiss();             /*for(int = 0; < myimages.size(); ++)             {                 iv_photo.setimagebitmap(myimages.get(i));             }*/         }           public bitmap decodefile(string filepath, int width, int hight) {             try {                  file f = new file(filepath);                  bitmapfactory.options o = new bitmapfactory.options();                 o.injustdecodebounds = true;                 bitmapfactory.decodestream(new fileinputstream(f), null, o);                  final int required_width = width;                 final int required_hight = hight;                 int scale = 1;                 while (o.outwidth / scale / 2 >= required_width                         && o.outheight / scale / 2 >= required_hight)                     scale *= 2;                  bitmapfactory.options o2 = new bitmapfactory.options();                 o2.insamplesize = scale;                 return bitmapfactory.decodestream(new fileinputstream(f), null, o2);             } catch (filenotfoundexception e) {                 e.printstacktrace();             }             return null;         }     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -