问题描述
我有我的ListView长preSS选项,将删除长pressed分贝条目。删除不实际工作。如果我出来的应用程序,并回去我跟我的没有行的消息psented $ P $。完美的。
在我的code我首先调用DBadapter方法来删除行。然后,我打电话的dbadapter一个fetchallnotes方法来刷新列表视图我。最后一行是不会消失的,但是,在这个fetchallnotes调用。只有在fetchallnotes在我的onCreate调用。
下面是我的上下文菜单调用哪个叫我DBAdapter方法
db.deleteNote(info.id);
getSnapz();
在DBAdapter这是我的我的deleteNote方法
公共布尔deleteNote(长ROWID){
返回db.delete(DATABASE_TABLE,KEY_ROWID +=+ ROWID,NULL)> 0;
}
这是我的getSnapz方法,这是我的主要业务是在我的列表视图是
公共无效getSnapz(){
尝试{
光标C = db.fetchAllNotes();
//重新查询光标柜面的最后一个条目已被删除
c.requery();
startManagingCursor(C);
如果(c.moveToFirst()){ 的String [] =由新的String [] {DBAdapter.KEY_TYPE,DBAdapter.KEY_WEIGHT,DBAdapter.KEY_DATE,DBAdapter.KEY_PLACE};
INT []为= INT新[] {R.id.typecol,R.id.weightcol,R.id.datecol,R.id.placecol}; SimpleCursorAdapter简单=新SimpleCursorAdapter(这一点,R.layout.snapz_row,C,从,到);
setListAdapter(简单);
} }赶上(例外五){
Log.e(TAG,e.toString());
}
}
这是我的fetchAllNotes在DBAdapter方法
公共光标fetchAllNotes()抛出异常{
光标C = db.query(DATABASE_TABLE,新的String [] {KEY_ROWID,KEY_URI,KEY_DATE,为key_type,KEY_WEIGHT,
KEY_PLACE},
NULL,NULL,NULL,NULL,NULL); 返回℃;
}
您删除的最后一行后,返回的游标是空的。因此c.moveToFirst()返回false,你的整个if块被跳过,并且您的列表视图保持不变,仍然呈现出刚刚删除的行。
它的工作原理,当你删除几行之一,因为moveToFirst()只要仍然有剩下的另一行返回成功。
当您退出并重新开始,因为该点的活动,并重新在ListView开始初始化它的工作原理。
解决方案:完全删除if语句。 SimpleCursorAdapter应该是快乐与0行的光标。
长期更好,但迁移到ContentProvider的,并使用ContentObserver模型,以确保您的游标更新自己当数据库它们反映的变化。
I have a long press option on my listview which will delete the long pressed db entry. The delete does actually work. If i come out of the app and go back in i am presented with my 'no rows' message. Perfect.
In my code I first call the DBadapter method to delete a row. I then call a fetchallnotes method in the dbadapter to refresh my listview. The last row won't disappear, however, on this fetchallnotes call.. Only on the fetchallnotes call in my onCreate.
Here is my Context Menu call which calls my DBAdapter method
db.deleteNote(info.id);
getSnapz();
This my my deleteNote method in DBAdapter
public boolean deleteNote(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
here is my getSnapz method, which is in my Main activity which is where my listview is
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());
}
}
And here is my fetchAllNotes method in DBAdapter
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;
}
After you delete the last row, the cursor returned is empty. Therefore c.moveToFirst() returns false, your whole if block is skipped, and your listview is left unchanged, still showing the just-deleted row.
It works when you delete one of several rows because moveToFirst() returns success as long as there was another row still left.
It works when you exit and resume because at that point the activity is recreated and the listview starts uninitialized.
Solution: drop the if statement completely. SimpleCursorAdapter should be happy with a cursor with 0 rows.
Long term better yet, migrate to a ContentProvider and use the ContentObserver model to make sure your cursors update themselves when the database they reflect changes.
这篇关于SQLite的删除最后一行刷新不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!