android - Retrieving an Image(saved in sqlite) from onClickListener -
i created book app, can save books internet (googlebooksapi). bookdata (title, author, image) saved in sqlite db. can view books in listview. if click on row ( book) bookdata shown in new activity (bookinfoactivity) problem is, dont know how pass image of row clicked bookinfoactivity.
i tried following, , know pass image of first row.
bookinfoactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_book_info); bookdbhelper = new bookdbhelper(this); typeface mytypeface = typeface.createfromasset(getassets(), "lobster.ttf"); textview mytextview = (textview) findviewbyid(r.id.yourbookinfo); mytextview.settypeface(mytypeface); imageview = (imageview) findviewbyid(r.id.passed_bookimage); btn_update = (button) findviewbyid(r.id.button_update); btn_delete_book = (button) findviewbyid(r.id.btn_delete_book); intent intent = getintent(); string secret_edittext_titel = intent.getstringextra(bookdatalistactivity.extra_msg0); cursor2.movetofirst(); int rows = cursor2.getcount(); for(int i=0; < rows; i++ ){ string booktitle = cursor2.getstring(1); byte[] blob= cursor2.getblob(0); bitmap bookimage = bitmapfactory.decodebytearray(blob, 0, blob.length); imageview.setimagebitmap(bookimage); toast.maketext(this, "here image", toast.length_short).show(); if(secret_edittext_titel == booktitle){ }else{ cursor2.movetonext(); } } /*bookdbhelper = new bookdbhelper(getapplicationcontext()); sqlitedatabasebooks = bookdbhelper.getreadabledatabase(); cursor2 = bookdbhelper.getinformations(sqlitedatabasebooks); if (cursor2.movetoprevious()) { { string se byte[] blob= cursor2.getblob(0); bitmap bookimage = bitmapfactory.decodebytearray(blob, 0, blob.length); imageview.setimagebitmap(bookimage); toast.maketext(this, "here image", toast.length_short).show(); } }*/ string secret_title = intent.getstringextra(bookdatalistactivity.extra_msg1); secret_edittext_title = (edittext) findviewbyid(r.id.secret_edittext_title); secret_edittext_title.settext(secret_title); //defaultimage = (imageview) findviewbyid(r.id.defaultimage); /*bitmap book_image = intent.????????(bookdatalistactivity.extra_msg0); passedbookimage = (imageview) findviewbyid(r.id.passed_bookimage); passedbookimage.setimagebitmap(book_image); */ string book_title = intent.getstringextra(bookdatalistactivity.extra_msg1); passedbooktitle = (edittext) findviewbyid(r.id.passed_booktitle); passedbooktitle.settext(book_title); string book_author = intent.getstringextra(bookdatalistactivity.extra_msg2); passedbookauthor = (edittext) findviewbyid(r.id.passed_bookauthor); passedbookauthor.settext(book_author); string book_date = intent.getstringextra(bookdatalistactivity.extra_msg3); passedbookdate = (edittext) findviewbyid(r.id.passed_bookdate); passedbookdate.settext(book_date); string book_rating = intent.getstringextra(bookdatalistactivity.extra_msg4); passedbookrating = (edittext) findviewbyid(r.id.passed_bookrating); passedbookrating.settext(book_rating); string book_shelf = intent.getstringextra(bookdatalistactivity.extra_msg5); passedbookshelf = (edittext) findviewbyid(r.id.passed_bookshelf); passedbookshelf.settext(book_shelf); deletedata(); }
*bookdbhelper
public class bookdbhelper extends sqliteopenhelper{ public sqlitedatabase sqlitedatabase; private static final string database_books_name = "bookinfo.db"; private static final int database_books_vers = 2; private static final string create_query_books = "create table " + bookcontent.newbookinfo.table_name_books +"(" + bookcontent.newbookinfo.book_image +" blob not null, " + bookcontent.newbookinfo.book_title+" text, " + bookcontent.newbookinfo.book_author+" text, " + bookcontent.newbookinfo.book_date+" text, " + bookcontent.newbookinfo.book_rating+" text, " + bookcontent.newbookinfo.book_shelf+" text);"; public bookdbhelper(context context){ super(context, database_books_name, null, database_books_vers); log.e("database operations", " database created"); } @override public void oncreate(sqlitedatabase bookdb) { bookdb.execsql(create_query_books); log.e("database operations", " database created"); } @override public void onupgrade(sqlitedatabase bookdb, int oldversion, int newversion) { bookdb.execsql(" drop table exists " + bookcontent.newbookinfo.table_name_books); oncreate(bookdb); } public void addinformations(byte[] image, string booktitle, string bookauthor, string bookdate, string bookrating, string bookshelf, sqlitedatabase bookdb) { contentvalues contentvalues = new contentvalues(); contentvalues.put(bookcontent.newbookinfo.book_image, image); contentvalues.put(bookcontent.newbookinfo.book_title, booktitle); contentvalues.put(bookcontent.newbookinfo.book_author, bookauthor); contentvalues.put(bookcontent.newbookinfo.book_date, bookdate); contentvalues.put(bookcontent.newbookinfo.book_rating, bookrating); contentvalues.put(bookcontent.newbookinfo.book_shelf, bookshelf); bookdb.insert(bookcontent.newbookinfo.table_name_books, null, contentvalues); log.e("database operations", "on row inserted"); } public void addownbookinformations(string booktitle, string bookauthor, string bookdate, string bookrating, string bookshelf, sqlitedatabase bookdb) { contentvalues contentvalues = new contentvalues(); contentvalues.put(bookcontent.newbookinfo.book_title, booktitle); contentvalues.put(bookcontent.newbookinfo.book_author, bookauthor); contentvalues.put(bookcontent.newbookinfo.book_date, bookdate); contentvalues.put(bookcontent.newbookinfo.book_rating, bookrating); contentvalues.put(bookcontent.newbookinfo.book_shelf, bookshelf); bookdb.insert(bookcontent.newbookinfo.table_name_books, null, contentvalues); log.e("database operations", "on row inserted"); } public cursor getinformations(sqlitedatabase bookdb){ cursor cursor2; string[] projections = { bookcontent.newbookinfo.book_image, bookcontent.newbookinfo.book_title, bookcontent.newbookinfo.book_author, bookcontent.newbookinfo.book_date, bookcontent.newbookinfo.book_rating, bookcontent.newbookinfo.book_shelf}; cursor2 = bookdb.query(bookcontent.newbookinfo.table_name_books, projections,null, null, null, null, null); return cursor2; }
bookdatalistactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.book_data_list_layout); typeface mytypeface = typeface.createfromasset(getassets(), "lobster.ttf"); textview mytextview = (textview) findviewbyid(r.id.text_yourbooks); mytextview.settypeface(mytypeface); btn_home = (button) findviewbyid(r.id.btn_home); booklistview = (listview) findviewbyid(r.id.book_list_view); inputsearch = (edittext) findviewbyid(r.id.search_bar); imageview = (imageview) findviewbyid(r.id.book_image); booklistdataadapter = new booklistdataadapter(getapplicationcontext(), r.layout.row_book_layout); booklistview.setadapter(booklistdataadapter); //onitemclicklistener booklistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(getapplicationcontext(), bookinfoactivity.class); /*imageview = (imageview) findviewbyid(r.id.book_image); byte[] book_image = cursor2.getblob(cursor2.getcolumnindex(bookcontent.newbookinfo.book_image)); imageview.setimagebitmap(bitmapfactory.decodebytearray(book_image,0,book_image.length)); //intent.putextra(extra_msg0, book_image); */ edittextbooktitle = (textview) view.findviewbyid(r.id.text_book_title); string book_title = edittextbooktitle.gettext().tostring(); intent.putextra(extra_msg1, book_title); edittextbookauthor = (textview) view.findviewbyid(r.id.text_book_author); string bookauthor = edittextbookauthor.gettext().tostring(); intent.putextra(extra_msg2, bookauthor); edittextbookdate = (textview) view.findviewbyid(r.id.text_book_date); string bookdate = edittextbookdate.gettext().tostring(); intent.putextra(extra_msg3, bookdate); edittextbookrating = (textview) view.findviewbyid(r.id.text_book_rating); string bookrating = edittextbookrating.gettext().tostring(); intent.putextra(extra_msg4, bookrating); edittextbookshelf = (textview) view.findviewbyid(r.id.text_book_shelf); string bookshelf = edittextbookshelf.gettext().tostring(); intent.putextra(extra_msg5, bookshelf); startactivity(intent); onresume(); } }); inputsearch.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { booklistdataadapter.getfilter().filter(s); } @override public void aftertextchanged(editable s) { } }); bookdbhelper = new bookdbhelper(getapplicationcontext()); sqlitedatabasebooks = bookdbhelper.getreadabledatabase(); cursor2 = bookdbhelper.getinformations(sqlitedatabasebooks); if (cursor2.movetofirst()) { { string booktitle, bookauthor, bookdate, bookrating, bookshelf; bitmap bookimage; byte[] blob= cursor2.getblob(0); bookimage = bitmapfactory.decodebytearray(blob,0,blob.length); booktitle = cursor2.getstring(1); bookauthor = cursor2.getstring(2); bookdate = cursor2.getstring(3); bookrating = cursor2.getstring(4); bookshelf = cursor2.getstring(5); bookdataprovider bookdataprovider = new bookdataprovider(bookimage, booktitle, bookauthor, bookdate, bookrating, bookshelf); booklistdataadapter.add(bookdataprovider); } while (cursor2.movetonext()); } backtomainview(); } public void backtomainview(){ btn_home.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(intent); } }); } @override public void onresume(){ super.onresume(); booklistdataadapter.notifydatasetchanged(); } }
logcat*
process: com.kasutwentyseven.gui4selfshelf, pid: 20482 java.lang.runtimeexception: unable start activity componentinfo{com.kasutwentyseven.gui4selfshelf/com.kasutwentyseven.gui4selfshelf.books.bookinfoactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.cursor android.database.sqlite.sqlitedatabase.query(java.lang.string, java.lang.string[], java.lang.string, java.lang.string[], java.lang.string, java.lang.string, java.lang.string)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2357) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2419) @ android.app.activitythread.access$900(activitythread.java:154) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5291) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.cursor android.database.sqlite.sqlitedatabase.query(java.lang.string, java.lang.string[], java.lang.string, java.lang.string[], java.lang.string, java.lang.string, java.lang.string)' on null object reference @ com.kasutwentyseven.gui4selfshelf.books.bookdbhelper.getinformations(bookdbhelper.java:85) @ com.kasutwentyseven.gui4selfshelf.books.bookinfoactivity.oncreate(bookinfoactivity.java:60) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2310) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2419) @ android.app.activitythread.access$900(activitythread.java:154) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5291) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699)
there lot more , explain whole sqlite implementation beyond frame here. first have implement unique id every book database. have pass id via intent next activity, show book info:
pass id listview activity:
intent myintent = new intent(yourlistviewactivity.this, yourbookactivity.class); myintent.putextra("bookid", id_value); startactivity(myintent);
get in book activity:
intent mintent = getintent(); int idvalue= mintent.getintextra("bookid", 0);
if got all, it´s easy this:
//set cursor first row cursor2.movetofirst(); //get number of rows int rows = cursor2.getcount(); //loop through cursor for(int i=0;i<rows;i++){ //get id int id = cursor2.getint(0); byte[] blob= cursor2.getblob(1); bitmap bookimage = bitmapfactory.decodebytearray(blob, 0, blob.length); imageview.setimagebitmap(bookimage); toast.maketext(this, "here image", toast.length_short).show(); if(idvalue==id){ //stop cursor here because wanted }else{ cursor2.movetonext(); } }
but that´s possible example, there other ways too. should read how build correct database intention:
http://examples.javacodegeeks.com/android/core/database/android-database-example/
Comments
Post a Comment