I am having an odd problem deleting rows from a sqlite database. The first time I try deleting a row, it seems to work fine. However, anytime thereafter, I get no errors, it simply does not remove the selected row. If I uninstall the app and re-run it from eclipse (I have my tablet hooked up to my pc for testing) then the first delete works again.
Here is the code that I am using to populate a Gallery View from my database
Cursor cursor = _garmentAdapter.fetchAllLooks();
cursor.moveToLast();
final ArrayList<Bitmap> al = new ArrayList<Bitmap>();
_labels=new ArrayList<String>();
int totalGarments=0;
for(int i=cursor.getCount()-1; i>=0; i--) {
totalGarments++;
Bitmap bd=BitmapFactory.decodeFile(cursor.getString(3));
String label=cursor.getString(1);
al.add(bd);
_labels.add(label);
cursor.moveToPrevious();
}
Toast.makeText(BrowseLooks.this, "ITEMS=="+totalGarments, Toast.LENGTH_SHORT).show();
This toast message is displaying the number of items on screen. For example, if I create 3 items, then delete 1, it shows 2 on screen and the toast says ITEMS==2. However, if I delete another one, I still have 2 items on screen and ITEMS==2. So it seems the cursor is working correctly, but the database is not.
Here is the code for the delete row in the database:
return mDb.delete(LOOKS_TABLE, KEY_ROWID+"="+rowId, null)>0;
I have also logged rowId as being correct when mDb is called.
Once again, I am not getting any errors, the code is just not working. Any help would be very much appreciated.
I have found the answer to my problem. I did not fully understand the autoincrement of the sqlite database. Because of that, I was deleting rows that were already empty. The way the database increments is to create an entry higher than all previous entries not just the current highest entry. Therefore, the indexes of my on-screen items did not correspond to the indexes in my database table.