1

Hey guys I have a list menu that's created from a strings column of a database . When a user clicks on a list menu item, they will delete it. I'm doing this by passing the list item position to the delete function on my DBAdapter.

protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  db.deleteContact(position);
  }

The function finds the corresponding row index and deletes it.

public boolean deleteContact(long rowId) {
  return db.delete(DATABASE_TABLE, KEY_NAME + "=" + rowId, null) > 0;
  }

The problem (which is probably very obvious to most of you) is that when a user deletes, lets say list menu item 2, and the corresponding index is deleted from the table, the list menu refreshes and the item previously in position 3 is now in position 2. if the user tries to delete that, the delete function receives "2" as a parameter but the index of the desired row to be deleted is 3.

I know this is a silly problem, but can someone suggest an effective workaround? thanks

2
  • i don't understand what the issue is. what would you rather happen? that your item is deleted off your database but not the listview? Also, why would you pass int position into your method when your delete method deletes based on long id, also supplied. Commented Nov 14, 2012 at 3:03
  • If I have 5 rows, and I click the middle one to delete it it works just fine. However, the list refreshes so that the range is always between 1-5, 1-4, 1-3 as items are deleted. Thats fine too. But the Index of the rows remains the same, i.e if I delete row 3, I'll be left with 1,2,4,5 but the user still submits a request for row 3 to be deleted of he clicks the 3rd item in the refreshed list. So nothing happens. Commented Nov 14, 2012 at 3:12

1 Answer 1

2

You shouldn't delete by position. The id provided in the onListClickItem method is here to help you. Replace your code above by:

protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  db.deleteContact(id);
  }

provided that your KEY_NAME is the primary key of your table. If not, replace KEY_NAME by the name of the primary key field.

Sign up to request clarification or add additional context in comments.

3 Comments

That makes perfect sense! I'll try that. Thanks edit: Worked, thanks so much for your help
Note: this will work as expected if you follow the standard android documentation on how to populate the list using a ListAdapter and a valid Cursor.
Yeah you're right, the list is populated with a ListAdapter and it seems to have a similar problem after testing. Maybe I should pass something else to the delete function. Thanks for your input all the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.