java - Get filename from database and use it to populate an ImageView -


i'm creating app @ point needs filename sqlite database, image filename , insert in imageview, i've created code right now, think i'm doing wrong here...

database db = new database(getapplicationcontext()); sqlitedatabase readabledatabase = db.getreadabledatabase(); final cursor curfilename = readabledatabase.rawquery("select filename data order id asc", null); image = (imageview) findviewbyid(r.id.image); int filename = curfilename.getcolumnindex("filename"); string filenamestring = curfilename.getstring(filename); d = drawable.createfrompath(environment.getexternalstoragedirectory().tostring()+filenamestring); toast.maketext(getapplicationcontext(), filenamestring, toast.length_short).show(); handledrawable(d); 

yes sorry, exception:

04-09 13:48:16.950: e/androidruntime(30802): fatal exception: main 04-09 13:48:16.950: e/androidruntime(30802): java.lang.runtimeexception: unable start activity componentinfo{g.d.filer/g.d.filer.favorites}: android.database.cursorindexoutofboundsexception: index -1 requested, size of 33 

you must position cursor before can read

 int filename = curfilename.getcolumnindex("filename");  if (cur.movetofirst()) { // position cursor first item. false: empty resultset      string filenamestring = curfilename.getstring(filename);      d = drawable.createfrompath(environment.getexternalstoragedirectory().tostring()+filenamestring);      toast.maketext(getapplicationcontext(), filenamestring, toast.length_short).show();      handledrawable(d);      ...  } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -