sqlite delete last row refresh not working

前端 未结 1 1189
孤独总比滥情好
孤独总比滥情好 2021-01-28 03:38

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 wit

相关标签:
1条回答
  • 2021-01-28 04:29

    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.

    0 讨论(0)
提交回复
热议问题