Deleting item from SQLite deletes next item
<p>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 (&quot;1&quot;, &quot;2&quot; and &quot;3&quot;)</p>
<p>I press delete on item &quot;1&quot; but it deletes item &quot;2&quot;</p>
<p>I press delete item &quot;2&quot;, it deletes &quot;3&quot; but,</p>
<p>If I press delete &quot;3&quot; application crashes. I don't understand what is wrong. when I write</p>
 <pre class="lang-java prettyprint-override"><code>db.delete(books.get(info.position-1))                                    
</code></pre>
<p>inside MainActivity.java it gives me crash when I try to delete (app stops working).</p>
<p><strong>DBAdapter.java</strong></p>
  <pre class="lang-java prettyprint-override"><code>public void delete(String name)throws SQLException {
    SQLiteDatabase db = helper.getWritableDatabase();
    if (db == null) {
        return;
    }
    String[] whereArgs = new String[] { name };
    db.delete(&quot;m_TB&quot;, &quot;NAME&quot;+ &quot;=?&quot;, whereArgs);
    db.close();
}
</code></pre>
<p><strong>MainActivity.java</strong></p>
 <pre class="lang-java prettyprint-override"><code>public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(&quot;Delete&quot;);
}

public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    String name = info.toString();
    if (item.getTitle().equals(&quot;Delete&quot;)) {
        db.delete(books.get(info.position));
        books.remove(item);
        adapter.notifyDataSetChanged();

    }
    return true;
}
</code></pre>