Delete row and restore rowId autoincrement

筅森魡賤 提交于 2019-12-12 02:33:26

问题


I have a question concerning the deletion of a row in a sql database. My methods use the id from a onItemClick listener to identify the clicked object but as soon as one item is deleted, these id's don't work anymore because they don't match the id row in the database. Here's an example to clarify what I mean:

ListView in Android

  • Item 1 (id 0)
  • Item 2 (id 1)
  • Item 3 (id 2)

Database (id gets corrected with +1 to match up)

  • Item 1 (id 1)
  • Item 2 (id 2)
  • Item 3 (id 3)

After deletion

ListView

  • Item 1 (id 0)
  • Item 3 (id 1)

Database

  • Item 1 (id 1)
  • Item 3 (id 3)

When the Item 3 is clicked now, the entry in the database with the id 2 is chosen but it doesn't exist anymore which causes an exception

I'm looking for a way to restore the databases autoincrementing id column to

  • Item 1 (id 1)
  • Item 3 (id 2)

Edit: After some research I have to conclude that it is not possible to do that. I'll try to find something like an unique identifier which is stored in a separate table along with the current rowid of the requested object.


回答1:


You have to put the id's in your variable object and when you click a listitem, you have to find the object's id and delete it.

Check this complete tutorial. using lang translater.



来源:https://stackoverflow.com/questions/17638109/delete-row-and-restore-rowid-autoincrement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!