android - Implementing lazy loading for simplecursoradapter -
here getting stored picture paths database. showing images in gridview of paths. shown in code below, loading cursoradapter in asynctask. problem here first time okay takes time until images loaded.
but if user adds 1 more pic database, again takes time load images kept asynctask in onresume() make added picture visible. same case whenever user adds each picture. can suggest me simple way last added picture visible in gridview without having load pictures again. or in least case can 1 me implementing lazy adapter present cursoradapter if not possible?
public class imagesscreen extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.images_screen); gridview = (gridview)findviewbyid(r.id.gridview1); string[] = new string[] { reminders.mom_paths }; int[] = new int[] {}; dbcon = new sqliteconnectclass(imagesscreen.this); imagecursoradapter = new imagecursoradapter(imagesscreen.this, r.layout.griditem, null, from, to); gridview.setadapter(imagecursoradapter); } protected void onresume() { super.onresume(); new getimages().execute((object[]) null); } private class getimages extends asynctask<object, object, cursor> { @override protected cursor doinbackground(object... params) { return dbcon.getallimages(); } @override protected void onpostexecute(cursor result) { imageadapter.changecursor(result); } public void addpictures(view view){ // code adding picture paths database transferring activity. } } customadapter class:
public class imagecursoradapter extends simplecursoradapter{ private int layout; private layoutinflater mlayoutinflater; private context mcontext; public imageadapter(context context, int layout, cursor c,string[] from, int[] to) { super(context, layout, c, from, to,0); this.layout = layout; mlayoutinflater=layoutinflater.from(context); mcontext = context; } public view newview(context context, cursor cursor, viewgroup parent) { view v = mlayoutinflater.inflate(layout, parent, false); return v; } public void bindview(final view v, final context context, cursor c) { final int id = c.getint(c.getcolumnindex("id")); final string imagepath = c.getstring(c.getcolumnindex("paths")); imageview imageview = (imageview)v.findviewbyid(r.id.imageview1); file imgfile = new file(imagepath); if(imgfile.exists()){ bitmap imagebitmap = decodefile(imgfile); imageview.setimagebitmap(imagebitmap); } } }
use code
public class imagegallery extends activity implements onitemclicklistener, onscrolllistener, ontouchlistener { button btn; sqlitedatabase sampledb = null; string sample_db_name = "hic"; int size = 0; textview headertext; button cartbtn; imageview footer1, footer2, footer3, footer4, footer5; gridview gridview; boolean larg = false; string products; int j = 1; int ii = 0; string imagepath[]; string productname[]; int productid[] = new int[1000]; string productids[] = new string[1000]; int integerprodids[] = new int[1000]; final context context = this; string filename2[]; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.currentspecial); file root = new file(environment.getexternalstoragedirectory() + file.separator + "aiwhic/product" + file.separator); file[] filename = root.listfiles(); filename2 = new string[filename.length]; (int j = 0; j < filename.length; j++) { uri uri = uri.fromfile(filename[j]); filename2[j] = filename[j].getabsolutepath(); log.e("file", filename2[j]); } gridview = (gridview) findviewbyid(r.id.gridview1); gridview.setadapter(new imageadapter(this, filename2, filename2)); gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { int isf = filename2[position].lastindexof("/"); int laf = filename2[position].lastindexof("."); string filename = filename2[position].substring(isf + 1, laf); int pos = integer.parseint(filename); intent go = new intent(imagegallery.this, itemspage.class); bundle bundle = new bundle(); bundle.putint("position", pos); bundle.putstring("whichclass", "imagegallery"); bundle.putint("catid", 2); go.putextras(bundle); startactivity(go); } }); } public class imageadapter extends baseadapter { private context context; private final string[] mobilevalues; private final string[] mobileimages; public imageadapter(context context, string[] mobilevalues, string[] mo) { this.context = context; this.mobilevalues = mobilevalues; this.mobileimages = mo; } public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view gridview; if (convertview == null) { gridview = new view(context); gridview = inflater.inflate(r.layout.currentspeciallist, null); textview textview = (textview) gridview .findviewbyid(r.id.textview1); textview.settext(mobilevalues[position]); textview.setvisibility(view.invisible); imageview imageview = (imageview) gridview .findviewbyid(r.id.imageview1); imageview.settag(mobileimages[position]); new loadimage().execute(imageview); // imageview.setimagebitmap(bitmapfactory // .decodefile(mobileimages[position])); } else { gridview = (view) convertview; } return gridview; } public int getcount() { return mobileimages.length; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } } class loadimage extends asynctask<object, void, bitmap> { private imageview imv; private string path; @override protected bitmap doinbackground(object... params) { imv = (imageview) params[0]; path = imv.gettag().tostring(); // bitmap thumb = bitmapfactory.decodefile(path); bitmapfactory.options opts = new bitmapfactory.options(); opts.insamplesize = 2; // 1/2 image loaded bitmap thumb = bitmap.createscaledbitmap( bitmapfactory.decodefile(path, opts), 120, 120, false); return thumb; } @override protected void onpostexecute(bitmap bitmap) { if (!imv.gettag().tostring().equals(path)) { /* * path not same. means image view * handled other async task. don't , * return. */ return; } if (bitmap != null && imv != null) { imv.setvisibility(view.visible); imv.setimagebitmap(bitmap); } else { imv.setvisibility(view.gone); } } } public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { } then create xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <imageview android:id="@+id/imageview1" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:src="@drawable/ic_launcher" > </imageview> </relativelayout> i getting file path directly . u name sqlite , pass array string
Comments
Post a Comment