java - My Recyclerview list stopped working after implementing SQLite database -
i'm building list app. i'm learning sqlite databases , i'm trying save user input sqlite database table , display in recyclerview list. when did without sqlite, worked problem implementation of database table. app supposed add data list per button click:
addingitems.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(itemsinput.gettext() != null){ items.add(new todo(itemsinput.gettext().tostring())); itemsinput.settext(""); }else { toast.maketext(mainactivity.this, "please enter do", toast.length_short).show(); } itemadapter.notifydatasetchanged(); } });
but not that. app not crash. problem list shows absolutely no data. following relevant java files:
mainactivity.java
public class mainactivity extends appcompatactivity { private sqlitedatabase sqlitedatabase; private list<todo> items = new arraylist<>(); private itemadapter itemadapter; private recyclerview listitemsrecyclerview; edittext itemsinput; button addingitems; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); itemsinput = (edittext)findviewbyid(r.id.to_do_edittext); addingitems = (button)findviewbyid(r.id.to_do_btn); listitemsrecyclerview = (recyclerview) findviewbyid(r.id.to_do_list); todolistdatabasehelper databasehelper = new todolistdatabasehelper(this); sqlitedatabase = databasehelper.getwritabledatabase(); items = new arraylist<>(); recyclerview.layoutmanager layoutmanager = new linearlayoutmanager(getapplicationcontext()); listitemsrecyclerview.setlayoutmanager(layoutmanager); listitemsrecyclerview.setitemanimator(new defaultitemanimator());; itemadapter = new itemadapter(items); listitemsrecyclerview.setadapter(itemadapter); new itemtouchhelper(new itemtouchhelper.simplecallback(0, itemtouchhelper.left | itemtouchhelper.right) { @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { return false; } @override public void onswiped(recyclerview.viewholder viewholder, int direction) { } }); addingitems.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(itemsinput.gettext().length() > 0){ addnewtodo(itemsinput.gettext().tostring()); itemsinput.settext(""); }else { toast.maketext(mainactivity.this, "please enter do", toast.length_short).show(); } itemadapter.notifydatasetchanged(); } }); } private list<todo> getalltodos(){ cursor cursor = sqlitedatabase.rawquery("select * " + todocontract.todoentry.table_name, new string[]{}); list<todo> todos = new arraylist<>(); todo todo; if (cursor != null && cursor.getcount() > 0) { if (cursor.movetofirst()) { { string name = cursor.getstring(cursor.getcolumnindex(todocontract.todoentry.column_todo_name)); int priority = cursor.getint(cursor.getcolumnindex(todocontract.todoentry.column_todo_priority)); int timestamp = cursor.getint(cursor.getcolumnindex(todocontract.todoentry.column_todo_timestamp)); todo = new todo(name); todos.add(todo); } while (cursor.movetonext()); } cursor.close(); } return todos; } private long addnewtodo(string name){ contentvalues cv = new contentvalues(); cv.put(todocontract.todoentry.column_todo_name, name); return sqlitedatabase.insert(todocontract.todoentry.table_name, null, cv); } }
my custom adapter:
public class itemadapter extends recyclerview.adapter<itemadapter.viewholder> { private list<todo> todolist; public class viewholder extends recyclerview.viewholder { public textview todotextview; public viewholder(view itemview) { super(itemview); todotextview = (textview) itemview.findviewbyid(r.id.to_do); } } public itemadapter(list<todo> todolist) { this.todolist = todolist; } @override public itemadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item, parent, false); return new viewholder(itemview); } @override public void onbindviewholder(itemadapter.viewholder holder, int position) { todo todo = todolist.get(position); holder.todotextview.settext(todo.gettodo()); } @override public int getitemcount() { return todolist.size(); } }
i ran unit tests on sqlite classes there no issue them.
try this
public void update() { cursor.requery(); notifydatasetchanged(); }
Comments
Post a Comment