Android Sqlite - obtaining the correct database row id

Posted by Dan_Dan_Man on Stack Overflow See other posts from Stack Overflow or by Dan_Dan_Man
Published on 2012-12-07T22:09:19Z Indexed on 2012/12/07 23:04 UTC
Read the original article Hit count: 172

Filed under:
|
|
|

I'm working on an app that allows the user to create notes while rehearsing a play. The user can view the notes they have created in a listview, and edit and delete them if they wish.

Take for example the user creates 3 notes. In the database, the row_id's will be 1, 2 and 3. So when the user views the notes in the listview, they will also be in the order 1, 2, 3 (intially 0, 1, 2 before I increment the values). So the user can view and delete the correct row from the database.

The problem arises when the user decides to delete a note. Say the user deletes the note in position 2. Thus our database will have row_id's 1 and 3. But in the listview, they will be in the position 1 and 2. So if the user clicks on the note in position 2 in the listview it should return the row in the database with row_id 3. However it tries to look for the row_id 2 which doesn't exist, and hence crashes.

I need to know how to obtain the corresponding row_id, given the user's selection in the listview. Here is the code below that does this:

// When the user selects "Delete" in context menu
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case DELETE_ID:
        deleteNote(info.id + 1);
        return true;
    }
    return super.onContextItemSelected(item);
}

// This method actually deletes the selected note
private void deleteNote(long id) {
    Log.d(TAG, "Deleting row: " + id);
    mNDbAdapter.deleteNote(id);
    mCursor = mNDbAdapter.fetchAllNotes();
    startManagingCursor(mCursor);
    fillData();
    // TODO: Update play database if there are no notes left for a line.
}

// When the user clicks on an item, display the selected note
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    viewNote(id, "", "", true);

}

// This is where we display the note in a custom alert dialog. I've ommited
// the rest of the code in this method because the problem lies in this line:
// "mCursor = mNDbAdapter.fetchNote(newId);"
// I need to replace "newId" with the row_id in the database.
private void viewNote(long id, String defaultTitle, String defaultNote,
        boolean fresh) {

    final int lineNumber;
    String title;
    String note;

    id++;

    final long newId = id;

    Log.d(TAG, "Returning row: " + newId);

    mCursor = mNDbAdapter.fetchNote(newId);

    lineNumber = (mCursor.getInt(mCursor.getColumnIndex("number")));
    title = (mCursor.getString(mCursor.getColumnIndex("title")));
    note = (mCursor.getString(mCursor.getColumnIndex("note")));

            .
            .
            .
}

Let me know if you would like me to show anymore code. It seems like something so simple but I just can't find a solution.

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about android