multithreading - Load Images in GridView in a Synchronization - Android -


i have question, have gridview in loading images using imageview, these images it´s stored in sd, using runnable class loading. when scrolling through gridview images loaded in box corresponds (imageview), , in other tables, i'm making list of albums of music, image loaded album, load in , on, how make synchronize?, ie if 1 charge, not loaded another

i hope me understand

thanks

pd: sorry english

public view getview(int position, view convertview, viewgroup parent) {     view v = convertview;     if (v == null) {         layoutinflater vi = (layoutinflater) getcontext().getsystemservice("layout_inflater");         v = vi.inflate(r.layout.listitemalbum, null);     }      bnsong = songs.get(position);     if (bnsong != null) {         coveralbum = (imageview) v.findviewbyid(r.id.imgcover);         coveralbum.setscaletype(imageview.scaletype.center_crop);          if (bnsong.getalbum() == null){             coveralbum.setbackgroundresource(r.drawable.mksong);         }else{             try {                 runnable r = new runnable() {                     @override                     public void run() {                         try{                             thread.sleep(1000);                                                              storage storage = new storage();                                                             cover = storage.getopenimagen(bnsong.getalbum(), "album");                         }catch (exception e){                             e.printstacktrace();                         }                         coveralbum.post(new runnable() {                             @override                             public void run() {                                 coveralbum.invalidate();                                 coveralbum.setimagebitmap(null);                                 if (cover != null){                                     coveralbum.setimagebitmap(cover);                                 }else {                                     coveralbum.setbackgroundresource(r.drawable.mksong);                                 }                             }                         });                     }                 };                 new thread(r).start();             } catch (exception e) {                 coveralbum.setbackgroundresource(r.drawable.mksong);             }         }     }     return v; } 

there library problem, released google team called "volley". it's pretty new though. information here: https://developers.google.com/live/shows/474338138/.

for solving problem without library though: first, should using android asynctask, , executeonexecutor(thread_pool_executor) method., on runnable , thread. you're making new thread every view on screen, plus every time it's scrolled. if want runnables, use executorservice make thread pool, , post runnables there.

now, problem you're having image being overwritten, when view reused. need check, after downloading image, item @ view position has same album. best done tag on view.

public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) {     layoutinflater vi = (layoutinflater) getcontext().getsystemservice("layout_inflater");     v = vi.inflate(r.layout.listitemalbum, null); }  final song song = songs.get(position); if (song != null) {     final imageview coveralbum = (imageview) v.findviewbyid(r.id.imgcover);     coveralbum.setscaletype(imageview.scaletype.center_crop); // why not once, @ inflation?      final album album = song.getalbum();     coveralbum.settag(album);              if (album == null){         coveralbum.setbackgroundresource(r.drawable.mksong);     }else{         // should in full class extends asynctask         asynctask<album, void, bitmap> task = new asynctask<album, void, bitmap>() {             @override             public bitmap doinbackground(album... albums) {                 final album album = albums[0];                 try{                     storage storage = new storage();                                                     return storage.getopenimagen(album, "album");                 }catch (exception e){                     e.printstacktrace();                     return null;                 }             }              @override             protected void onpostexecute(bitmap cover) {                 final album currentalbum = (album) coveralbum.gettag();                 if(!album.equals(currentalbum))                     return;                  coveralbum.invalidate();                 coveralbum.setimagebitmap(null);                 if (cover != null){                     coveralbum.setimagebitmap(cover);                 }else {                     coveralbum.setbackgroundresource(r.drawable.mksong);                 }             }         }          task.executeonexecutor(asynctask.thread_pool_executor, album)     } } return v; 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -