android - Populating a listview with images from the SD card (not a set amount of items in list) -
basically i'm trying make contact list 1 provided android. when populating listview items, using simplecursoradapter can names appear in r.id.textview of each item:
private void filldata() { mcursor = mdbadapter.fetchallcontacts(); startmanagingcursor(mcursor); string[] = new string[] {dbadapter.key_name}; int[] = new int[] {r.id.contact_name}; simplecursoradapter contacts = new simplecursoradapter(this, r.layout.contact_view, mcursor, from, to); this.setlistadapter(contacts); }
something that. i've searched , found sample code both getting images online, or displaying set number of images in items (for instance know have 5 items 5 matching images). don't know i'd begin on getting images sd card, , displaying them in proper item. images named according id of contact, have means call proper image.
a push in right direction appreciated, thank you!
edit: @jeff gilfelt gave great answer, went ahead , spoke when saying figure out rest on own... haha. have default image declared in xml contacts android does. when implement new adapter, compresses items nothing, figure because finds empty bitmap @ location. did following:
@override public void setviewimage(imageview v, string id) { file root = environment.getexternalstoragedirectory(); file path = new file(root, "path/images/thumbs/"+id+".jpg"); if(path.exists()) { bitmap bitmap = bitmapstatic.getthumb(id); v.setimagebitmap(bitmap); } else { super.setviewimage(v, id); } }
but doesn't either. ideas?
edit2: figured out above problem. go this:
else { resources res = mcontext.getresources(); drawable drawable = res.getdrawable(r.drawable.default); v.setimagedrawable(drawable); }
hope helps others! remember solution have add context member var, , line mcontext = context
in constructor.
create subclass of simplecursoradaptor , override setviewimage method such constructs path appropriate file on sd card id feed it, use bitmapfactory.decodefile create bitmap use image view binding to:
public class mysimplecursoradapter extends simplecursoradapter { public mysimplecursoradapter(context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); } @override public void setviewimage(imageview v, string id) { string path = "/path/to/sd/card/" + id + ".png"; bitmap b = bitmapfactory.decodefile(path); v.setimagebitmap(b); } }
then add contact id cursor , view id of imageview to/from arrays pass adapter. example:
private void filldata() { mcursor = mdbadapter.fetchallcontacts(); startmanagingcursor(mcursor); string[] = new string[] {dbadapter.key_name, dbadapter.id}; int[] = new int[] {r.id.contact_name, r.id.contact_image}; mysimplecursoradapter contacts = new mysimplecursoradapter(this, r.layout.contact_view, mcursor, from, to); this.setlistadapter(contacts); }
Comments
Post a Comment