简体   繁体   中英

How to reset Database row ID in Android Back to 1 after Delete

I have a database adapter with relationship to a listview, i have a on click listener set up in when the list item is clicked the item is deleted after the database, the listivew resets back to 0, but the database doesn't.. I NEED TO KNOW THAT AFTER AI DELETE AND ITEM IN MY LIST HOW IN THE WORLD CAN I RESET THE DATABASE AUTO INCREMENT COUNTER FOR THE ROW ID BACK TO 1..otherwise the listview and the databse are back in sync..otherwise the delete method stops working! THIS IS ISSUE IS DRIVING ME CRAZY! PLEASE HELP!

//This is my code in the database adapter
public boolean deleteTask(long id){
     return checklistDB.delete(DATABASE_TABLE, KEY_ROWID+ "=" + id, null) > 0;
}


//This is me calling the method in my activity
listview2.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        checklistDB.open();
        //I add a counter to the position so that it equals the database row which starts at 1
        arg2++;
        checklistDB.deleteTask(arg2);
        startActivity(new Intent("com.aondo.checklist.buslist"));               
    }
});

The way SQLite works an autoincrementing ID will be consistent after rows are deleted (eg if you delete row id 2, row id 1 and 3 will keep the same value), so the only ways of making the IDs sequential again is to move entries manually around or copy the rows, drop the table, create a new one and inserting the rows again.

If you're using the local storage just for a view, however, you could always use the limit clause to get a select portion of your db for viewing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM