Reputation: 326
I'm working on the structure for a database, in Android Studio. I am not at the stages of testing, yet, but understanding smaller details in how it works would allow me to write a better structure sooner rather than later. Unfortunately, there is one small element that am unsure of that has proved rather difficult to look for, online.
I create an SQLiteDatabase
, and use it to store values that I represent in a class called Task
. When I enter each Task
into the database, I also retrieve the Id to store into my local container newTask
, with newTask.SetId(mTaskDataSource.Create(newTask))
. I ultimately use the SQLiteDatabase
to store my tasks across sessions, and use a RecyclerView
to display the list in an activity. This means that I have to keep track of the elements position in both the SQLiteDatabase
and the RecyclerView
.
If I delete an element in the database, will the consecutive element Ids "cascade down", or will they stay at their original values? If an example is needed, consider that my database stores elements, and displays the Ids. If I start with {0, 1, 2, 3}
, and remove element 2
, will my database balance its Ids back to {0, 1, 2}
, or will I have {0, 1, 3}
?
As I am working off the assumption that it balances back ({0, 1, 2}
), I have realised that I potentially set myself up for a lot of extra/redundant work, if my assumptions are wrong. If it does not balance in this way, is there a reasonably cheap way to do so?
I am asking if ID numbers will rebalance themselves. This is not the same as asking what the ID number is actually for, nor is the duplicate question of any valid use to my question. I am asking specifically in regards to an SQLiteDatabase
, not a generic Adapter
.
Upvotes: 0
Views: 121
Reputation: 81
If your ID is the primary key with autoincrement
modifed , the id will only increment by one based on the current max index . The Db will not reuse the ID you have used before .
Upvotes: -1
Reputation: 15107
The database does not change its ID, or any other data in other rows, when deleting one row. The ID is just for easier join queries and other things, so it must not be changed.
If you want to have a specific order, you do not need to use row ID. Data will be returned in the order you added them, so you can use their order index (row index in database) as the recycler view index.
Upvotes: 3