Search code examples
androidsqlitepositioncursor

Cursor delete position


WHY? please

select code

private void selectData() {

    String sql = "select ID, money, memo, date, type from " + _TABLENAME;
    mCursor = mDbOpenHelper.mDB.rawQuery(sql, null);
    mCursor.moveToFirst();
    while (!mCursor.isAfterLast()) {
        mi = new MyItem(
                mCursor.getString(0),
                mCursor.getString(3),
                mCursor.getString(2),
                mCursor.getString(1),
                mCursor.getString(4));
        arItem.add(mi);
        mCursor.moveToNext();
    }
    mCursor.close();
}

delete button click, delete code

@Override
public void onClick(DialogInterface dialog, int which) {
    /* problem */

    final String sql = "delete from "+_TABLENAME+" where ID = "+ selectPosition;
}
});

Solution

  • Your id is wrong, you should use the id you retervied via the cursor and not the position. You may try something like this

     String sql = "delete from "+_TABLENAME+" where ID = "+ mi.get(selectPosition).id;
    

    here the mi.get(selectedPosition) returns the corresponding object you added in selectData method and the .id gets the id you passed to it via the constructor. You should use your correct variable name to achieve this