android - Delete the entire table from sqLite database -
android - Delete the entire table from sqLite database -
this fragment have listview. here info read , entered in listview. set button here deleted entire info of particular table database. when press button, no response listview (i.e. listview itmes still there). when fragment restarted (either orientation changed or restarting entire app), info deleted.
public class taskslistfrag extends fragment implements view.onclicklistener { listview taskslist; private arraylist<singlerow> todoitems = new arraylist<>(); string taskentered, dateentered, timeentered; tasklistrecycler adapter; button delete; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.tasks_list_frag, container, false); log.d("hirakdebug", "taskslistfrag view created"); taskslist = (listview) view.findviewbyid(r.id.tasks_list); adapter = new tasklistrecycler(getactivity(), todoitems); taskslist.setadapter(adapter); log.d("hirakdebug", "taskslistfrag adapter set"); delete = (button) view.findviewbyid(r.id.deleteall); delete.setonclicklistener(this); homecoming view; } @override public void onresume() { super.onresume(); log.d("hirakdebug", "tasklistfrag resumed"); tasks_database_operations tasksdatabaseoperations = new tasks_database_operations(getactivity()); string[] columns = {tasksdatabaseoperations.assis_task, tasksdatabaseoperations.assis_date, tasksdatabaseoperations.assis_time}; sqlitedatabase sqlitedatabase = tasksdatabaseoperations.getwritabledatabase(); cursor cursor = sqlitedatabase.query(tasksdatabaseoperations.assis_task_table_name, columns, null, null, null, null, null); while (cursor.movetonext()) { log.d("hirakdebug", "cursor moved next"); int index0 = cursor.getcolumnindex(tasks_database_operations.assis_task); int index1 = cursor.getcolumnindex(tasks_database_operations.assis_date); int index2 = cursor.getcolumnindex(tasks_database_operations.assis_time); string task = cursor.getstring(index0); log.d("hirakdebug", "tasks_database_operations got task table"); string date = cursor.getstring(index1); log.d("hirakdebug", "tasks_database_operations got date table"); string time = cursor.getstring(index2); log.d("hirakdebug", "tasks_database_operations got time table"); singlerow ld = new singlerow(); ld.settask(task); log.d("hirakdebug", "tasks_database_operations settask called"); ld.setdate(date); log.d("hirakdebug", "tasks_database_operations setdate called"); ld.settime(time); log.d("hirakdebug", "tasks_database_operations settime called"); todoitems.add(ld); log.d("hirakdebug", "taskslistfrag info chenged notified"); adapter.notifydatasetchanged(); sqlitedatabase.close(); log.d("hirakdebug", "taskslistfrag database closed"); /* tasks_database_operations tasksdatabaseoperations = new tasks_database_operations(getactivity()); cursor cursor = tasksdatabaseoperations.readdata(); string[] = new string[]{tasksdatabaseoperations.assis_task, tasksdatabaseoperations.assis_date, tasksdatabaseoperations.assis_time}; int[] = new int[]{r.id.task_added, r.id.date_added, r.id.time_added}; simplecursoradapter simplecursoradapter = new simplecursoradapter(getactivity(),r.layout.single_row, cursor,from, to); simplecursoradapter.notifydatasetchanged(); taskslist.setadapter(simplecursoradapter);*/ } } @override public void onpause() { super.onpause(); log.d("lifecycle", "tlf pause"); } @override public void onstop() { super.onstop(); log.d("lifecycle", "tlf stop"); } @override public void ondestroyview() { super.ondestroyview(); log.d("lifecycle", "tlf destroyview"); } @override public void ondestroy() { super.ondestroy(); log.d("lifecycle", "tlf destroy"); } @override public void ondetach() { super.ondetach(); log.d("lifecycle", "tlf detach"); } @override public void onattach(activity activity) { super.onattach(activity); log.d("lifecycle", "tlf attach"); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("lifecycle", "tlf create"); } @override public void onstart() { super.onstart(); log.d("lifecycle", "tlf start"); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putserializable("list", (serializable) todoitems); log.d("lifecycle", "tlf saveinstance state"); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (savedinstancestate != null) { //probably orientation alter todoitems = (arraylist<singlerow>) savedinstancestate.getserializable("list"); } else { if (todoitems != null) { //returning backstack, info fine, nil } } } @override public void onclick(view v) { tasks_database_operations tasksdatabaseoperations = new tasks_database_operations(getactivity()); sqlitedatabase sqlitedatabase = tasksdatabaseoperations.getwritabledatabase(); sqlitedatabase.delete(tasksdatabaseoperations.assis_task_table_name, null, null); log.d("hirakdebug", "all info deleted"); adapter.notifydatasetchanged(); sqlitedatabase.close(); } }
you not clearing todolitems
. need clear after deleting table.
add below line before adapter.notifydatasetchanged()
within onclick()
method.
todolitems.clear();
android sqlite
Comments
Post a Comment