android - updating and deleting row in listview -
i know question not new, other examples found here didnt work. thats why writing you.
i created book application, can save bookdata googlebooks , can save data sqlite db. bookdata displayed in listview. if click on row, data of row displayed in edittexts in activity - looks detailed book view. example pictures: listview ; detailed bookview
now want give possibility user edit these bookinfos. implemented code update , delte info , info logcat, db created when go listview no data updated or deleted.
i used video called "android studio tutorial - 37 - update database" youtube convert solution
maybe can me.
following code book info:
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); btn_update = (button) findviewbyid(r.id.button_update); intent intent = getintent(); string secret_title = intent.getstringextra(bookdatalistactivity.extra_msg1); secret_edittext_title = (edittext) findviewbyid(r.id.secret_edittext_title); secret_edittext_title.settext(secret_title); 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); } public void updatebookinfo(view view){ bookdbhelper = new bookdbhelper(getapplicationcontext()); sqlitedatabasebooks = bookdbhelper.getwritabledatabase(); secret_edittext_titel = secret_edittext_title.gettext().tostring(); string booktitle, bookauthor, bookdate, bookrating, bookshelf; booktitle = passedbooktitle.gettext().tostring(); bookauthor = passedbookauthor.gettext().tostring(); bookdate = passedbookdate.gettext().tostring(); bookrating = passedbookrating.gettext().tostring(); bookshelf = passedbookshelf.gettext().tostring(); int count = bookdbhelper.updateeditedbookinfo(secret_edittext_titel, booktitle,bookauthor, bookdate, bookrating, bookshelf, sqlitedatabasebooks); toast.maketext(getapplicationcontext(), count+" book updated", toast.length_long).show(); finish(); } public void deletebook(view view){ bookdbhelper = new bookdbhelper(getapplicationcontext()); sqlitedatabasebooks = bookdbhelper.getwritabledatabase(); bookdbhelper.deletebookinformation(secret_edittext_titel, sqlitedatabasebooks); toast.maketext(getbasecontext(), "book deleted", toast.length_long).show(); }
and bookdbhelper:
public int updateeditedbookinfo(string old_title, string new_title, string new_author, string new_date, string new_rating, string new_shelf, sqlitedatabase sqlitedatabase){ contentvalues contentvalues = new contentvalues(); contentvalues.put(bookcontent.newbookinfo.book_title, new_title); contentvalues.put(bookcontent.newbookinfo.book_author, new_author); contentvalues.put(bookcontent.newbookinfo.book_date, new_date); contentvalues.put(bookcontent.newbookinfo.book_rating, new_rating); contentvalues.put(bookcontent.newbookinfo.book_shelf, new_shelf); string selection = bookcontent.newbookinfo.book_title + " = ?"; string [] selection_args = {old_title}; int count = sqlitedatabase.update(bookcontent.newbookinfo.table_name_books, contentvalues, selection, selection_args); return count; } public void deletebookinformation(string book_title,sqlitedatabase sqlitedatabase){ string selection = bookcontent.newbookinfo.book_title+ " =?"; string [] selection_args = {book_title}; sqlitedatabase.delete(bookcontent.newbookinfo.table_name_books, selection, selection_args); }
edit asked activity_book_info.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.kasutwentyseven.gui4selfshelf.books.bookinfoactivity" android:background="@color/background"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/yourbookinfo" android:id="@+id/yourbookinfo" android:textsize="25dp" android:textcolor="@color/textcolor" android:gravity="center_horizontal" android:textstyle="bold" /> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/yourbookinfo" android:layout_alignparentstart="true" android:id="@+id/linearlayout5"> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <imageview android:layout_width="100dp" android:layout_height="match_parent" android:id="@+id/passed_bookimage" /> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passed_booktitle" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passed_bookauthor" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passed_bookdate" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passed_bookrating" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passed_bookshelf" /> </linearlayout> </linearlayout> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout5" android:layout_alignparentstart="true" android:id="@+id/linearlayout6"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_update" android:id="@+id/button_update" android:onclick="updatebookinfo"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_delete" android:id="@+id/btn_delete_book" android:onclick="deletebook"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout6"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/secret_edittext_title" /> </linearlayout> </relativelayout>
with of @0x0nosugar (thanks that) found solution:
to updatebookinfo: added:
intent intent = new intent(getapplicationcontext(), bookdatalistactivity.class); startactivity(intent);
and deleting:
public void deletedata(){ btn_delete_book.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { integer deletedrows = bookdbhelper.deletedate(passedbooktitle.gettext().tostring()); if (deletedrows > 0) { toast.maketext(getapplicationcontext(), " book deleted", toast.length_long).show(); intent intent = new intent(getapplicationcontext(), bookdatalistactivity.class); startactivity(intent); finish(); } else { toast.maketext(getapplicationcontext(), " book not deleted", toast.length_long).show(); } } });
at bookdbhelper:
public integer deletedate (string book_title){ sqlitedatabase sqlitedatabase = this.getwritabledatabase(); string selection = bookcontent.newbookinfo.book_title+ " = ?"; string[] selection_args = {book_title}; return sqlitedatabase.delete(bookcontent.newbookinfo.table_name_books,selection, selection_args); }
method @ list activitiy:
@override public void onresume(){ super.onresume(); booklistdataadapter.notifydatasetchanged(); }
Comments
Post a Comment