我正在尝试管理Android应用程序中的简单列表。列表的内容保存在SQLite数据库中。当用户选择并保留特定行时,将出现带有“删除”选项的上下文菜单。当他们选择“删除”时,该行将从数据库中删除,但视图不会刷新。当我退出应用程序并重新进入时,相应的行已被删除。所以,我知道该行被删除,它只是ListView不刷新。
以下是代码的相关部分......
在onCreate方法中......
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}
));
在onContextItemSelected方法中......
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);
我错过了什么?
谢谢!