• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Android,sqlite auto increment problem

 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,I have an arraylist in my android app where I can see list of names like:

1.item
2.item
3.item
4.item
5.item
6.item
7.item ....
1,2,3,4,5,6,7 are coloumn ids in my sqlite database.Column id is set to primary key with auto increment .My problem is when I delete these items and add a new one ,list shows
8.item name(in place of 1.item name) even though there are no elements in it.Which is not appropriate from user's perspective.What I am expecting is ,whenever an item is deleted or all the items are deleted either the column ids should get updated or the newly added item should sit sequentially in the database.

my codes :
for listActivity

DBHelper class:
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dinnu goud wrote:What I am expecting is ,whenever an item is deleted or all the items are deleted either the column ids should get updated
If you want to renumber the ids in the database, then you need to renumber the ids - your delete code would have to be extended to get all items with a greater id and decrement them. I am not positive this is the best route to go though. I actually think the unique ids in the database should be independent of anything you display to the user. The user should be unaware of how the database increments or maintains its identifiers.

or the newly added item should sit sequentially in the database.
Databases and database queries are generally un-order collections of things - there is no 'sit sequentially'. You need to order your elements when you are querying for them. There is basic SQL syntax for doing this, so I will leave you to investigate it. Combining this with my response from above, you could order by the id, then use a value in Java that you increment inside your getAllNames() method which you present to the user. This would allow you to always present contiguous numbers starting at 1, regardless of the fill of your auto-incrementing database IDs. If I wanted to make sure items were always retrieved in the order they were inserted, I would add a timestamp column that I would use to order by, rather than the id, so that order can be preserved even if my ids are compressed, filled, recycled, or optimized.
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some how I managed to fix this issue.But it created one more issue.Thanks for your reply steve.My code for onItemClick():

but the problem with this approach is is, I must display an index to user which is bad(after deleting all the items in it, the index begins from where it stopped, instead of starting from the beginning).What i am looking for is when ever an item in the list is clicked,details pertaining to that item should display in the next activity which in my case is displayNames activity.All my application is completed except this.I am webbed here.Please help me get out of this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic