How to update ListView list after database change?

I am trying to manage a simple list in an android application. The contents of the list are stored in the SQLite database. When the user selects and holds in a certain line, a context menu appears with the "Delete" option. When they select "Delete", the row is deleted from the database, but the view is not updated. When I return from the application and return, the corresponding line will be deleted. So, I know that the row has been deleted, it is just a ListView that is not updating.

Here are the relevant bits of code ...

In the onCreate method ...

SQLiteDatabase db = tasks.getReadableDatabase(); Cursor cursor = db.query(TABLE_NAME, new String[] { _ID, TITLE, DETAILS, }, null, null, null, null, TITLE + " DESC"); startManagingCursor(cursor); ListView lv = (ListView) findViewById(R.id.list); lv.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {TITLE}, new int[] { android.R.id.text1} )); 

In the onContextItemSelected ... method

 switch(item.getItemId()) { case 0: SQLiteDatabase db = tasks.getWritableDatabase(); ListView lv = (ListView) findViewById(R.id.list); SimpleCursorAdapter adapter = (SimpleCursorAdapter) lv.getAdapter(); db.delete(TABLE_NAME, "_ID=?", new String[] {adapter.getCursor().getString(0)}); adapter.notifyDataSetChanged(); // Not working, clears screen and doesn't reload return true; } return super.onContextItemSelected(item); 

What am I missing?

Thanks!

+7
source share
1 answer

Get notifyDataSetChanged() of notifyDataSetChanged() and call requery() on Cursor . Here is an example project demonstrating this.

+5
source

All Articles