I have wrote code to delete an item from SQLite database and from listview, it always deletes next item. For example i have 3 items to delete ("1", "2" and "3") i press delete on item "1" but it deletes item "2", i press delete item "2", it deletes "3" but if i press delete "3" application crashes. I dont understand what is wrong. when i write
db.delete(books.get(info.position-1))
inside MainActivity.java it gives me crash when i try to delete (app stops working).
DBAdapter.java
public void delete(String name)throws SQLException {
SQLiteDatabase db = helper.getWritableDatabase();
if (db == null) {
return;
}
String[] whereArgs = new String[] { name };
db.delete("m_TB", "NAME"+ "=?", whereArgs);
db.close();
}
MainActivity.java
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("Delete");
}
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String name = info.toString();
if (item.getTitle().equals("Delete")) {
db.delete(books.get(info.position));
books.remove(item);
adapter.notifyDataSetChanged();
}
return true;
}
Aucun commentaire:
Enregistrer un commentaire