我的列表视图上有一个长按选项,它将删除长按的数据库条目。删除确实有效。如果我退出应用程序并返回,我会看到“无行”消息。完美的。
在我的代码中,我首先调用 DBadapter 方法来删除一行。然后,我在 dbadapter 中调用 fetchallnotes 方法来刷新我的列表视图。然而,在这个 fetchallnotes 调用中,最后一行不会消失。仅在我的 onCreate 中的 fetchallnotes 调用中。
这是我的上下文菜单调用,它调用我的 DBAdapter 方法
db.deleteNote(info.id);
getSnapz();
这是我在 DBAdapter 中的 deleteNote 方法
public boolean deleteNote(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
这是我的 getSnapz 方法,它位于我的主要活动中,这是我的列表视图所在的位置
public void getSnapz(){
try {
Cursor c = db.fetchAllNotes();
//requery cursor incase the last entry has been deleted
c.requery();
startManagingCursor(c);
if(c.moveToFirst()){
String[] from = new String[]{DBAdapter.KEY_TYPE,DBAdapter.KEY_WEIGHT,DBAdapter.KEY_DATE,DBAdapter.KEY_PLACE};
int[] to = new int[]{R.id.typecol,R.id.weightcol,R.id.datecol,R.id.placecol};
SimpleCursorAdapter simple = new SimpleCursorAdapter(this, R.layout.snapz_row, c, from, to);
setListAdapter(simple);
}
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}
这是我在 DBAdapter 中的 fetchAllNotes 方法
public Cursor fetchAllNotes() throws Exception{
Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_URI, KEY_DATE,KEY_TYPE,KEY_WEIGHT,
KEY_PLACE},
null, null, null, null, null );
return c;
}