simplecursoradapter - Android Where to call changecursor? -
i have custom simplecursoradapter getview this:
public view getview(int position, view convertview, viewgroup parent) { //this.changecursor(c); tried didn't work. this.c.movetoposition(position); int urlcol = c.getcolumnindex("url"); final string url = c.getstring(urlcol); //other code stuff. imagedownloader.download(url, thumbnail, rowid, db,pb);
the imagedownloader calls update on dbadapter c.getstring(urlcol) still gives me previous value. tried putting changecursor @ position above still got same value. should calling this? thanks!
i don't want listview redraw want latest data cursor. i'm passing in imageview , url imagedownloader class download image, set bitmap imageview , update database. noticed cursor not updated getview method returns me same data.
this custom simplecursoradapter declaration. getview mentioned above.
public class messagescursoradapter extends simplecursoradapter { private final imagedownloader imagedownloader = new imagedownloader();
imagedownloader class
public void download(string url, imageview imageview, int rowid, testdbadapter db,progressbar pb) { //url can http:// or /mnt/sdcard/mnt/ etc etc //some lenghty code check if exists physically on phone. else.... forcedownload(url, imageview, rowid, db,pb); private void forcedownload(string url, imageview imageview, int rowid, testdbadapter db,progressbar pb) { bitmapdownloadertask task = new bitmapdownloadertask(imageview, rowid, db,pb);
asynctask class in imagedownloader class
class bitmapdownloadertask extends asynctask<string, void, bitmap> { private string url; private int rowid; private testdbadapter db; private progressbar pb; private final weakreference<imageview> imageviewreference; public bitmapdownloadertask(imageview imageview, int rowid, testdbadapter db,progressbar pb) { imageviewreference = new weakreference<imageview>(imageview); this.rowid = rowid; this.db = db; this.pb = pb; } @override protected void onpreexecute() { // todo auto-generated method stub pb.setvisibility(view.visible); } @override protected bitmap doinbackground(string... params) { url = params[0]; return downloadbitmap(url); } @override protected void onpostexecute(bitmap bitmap) { if (iscancelled()) { bitmap = null; } if (imageviewreference != null) { imageview imageview = imageviewreference.get(); bitmapdownloadertask bitmapdownloadertask = getbitmapdownloadertask(imageview); if ((this == bitmapdownloadertask)) { int pos = url.lastindexof("/"); string filename = url.substring(pos, url.length()); system.out.println(filename); string extloc = environment.getexternalstoragedirectory() + "/test"; file folder = new file(extloc); if (!folder.exists()) { folder.mkdir(); } try { fileoutputstream out = new fileoutputstream(extloc + filename); boolean result = db.updatemessageurl(rowid, extloc + filename); //should update cursor here, purpose change url link physical location on phone. bitmap.compress(bitmap.compressformat.jpeg, 95, out); bitmapfactory.options options = new bitmapfactory.options(); options.intempstorage = new byte[24 * 1024]; options.injustdecodebounds = false; options.insamplesize = 2; bitmap thumbnail = bitmapfactory.decodefile(extloc + filename, options); imageview.setimagebitmap(bitmap); addbitmaptocache(url, bitmap); pb.setvisibility(view.gone); } catch (exception e) { e.printstacktrace(); } } } }
ok if looks weird, i'm hiding names.
first do:
c.movetofirst();
before access data rest want. in case call c.movetofirst()
, not in getview()
outside, since you'll same, because getview() called multiple times when performs populating. perhaps, call in method returning cursor.
when accessing, dont forget move cursor next data c.movetonext();
must inside getview();
Comments
Post a Comment