簡體   English   中英

Android SQLite-獲取正確的數據庫行ID

[英]Android Sqlite - obtaining the correct database row id

我正在開發一個允許用戶在排練劇本的同時創建筆記的應用程序。 用戶可以在列表視圖中查看他們創建的注釋,並根據需要編輯和刪除它們。

以用戶創建3個筆記為例。 在數據庫中,row_id分別為1、2和3。因此,當用戶在列表視圖中查看筆記時,它們的順序也將依次為1、2、3(在我遞增值之前通常為0、1、2)。 。 因此,用戶可以查看和從數據庫中刪除正確的行。

當用戶決定刪除筆記時,就會出現問題。 假設用戶刪除了位置2的注釋,因此我們的數據庫將具有row_id的1和3。但是在列表視圖中,它們將位於位置1和2。因此,如果用戶單擊列表視圖中位置2的注釋,應該返回數據庫中具有row_id 3的行。但是,它會嘗試查找不存在的row_id 2,從而導致崩潰。

考慮到列表視圖中的用戶選擇,我需要知道如何獲取相應的row_id。 下面是執行此操作的代碼:

// 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")));

            .
            .
            .
}

讓我知道您是否要顯示更多代碼。 看起來很簡單,但我找不到解決方案。

謝謝!

編輯

我解決了這個問題。 我回答了下面的問題。

好吧,我至少暫時解決了這個問題。

問題是我正在使用自己創建的適配器類。 它沒有跟蹤列表中的哪個項目與數據庫中的項目相對應。 現在,我已經使用SimpleCursorAdapter重寫了“ fillData()”方法。

這是OLD實現,帶有我自己的自定義適配器:

private void fillData() {
    String note;
    notes = new ArrayList<String>();

    // Populate arraylist with database data
    if (mCursor.moveToFirst()) {
        do {
            // Get current row's character and line
            note = mCursor.getString(mCursor.getColumnIndex("title"));
            Log.d(TAG, "Adding note: " + note);
            notes.add(note);
        } while (mCursor.moveToNext());
    }

    // Fill list with our custom adapter
    NoteAdapter adapter = new NoteAdapter(this, R.layout.note_row_layout,
            notes);
    setListAdapter(adapter);
}

這是用SimpleCursorAdapter撤銷的同一類:

private void fillData() {
    mFrom = new String[] { NoteDbAdapter.KEY_TITLE };
    mTo = new int[] { R.id.textNote };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.note_row_layout, mCursor, mFrom, mTo);

    setListAdapter(adapter);
}

之所以創建自己的適配器類,是因為列表的每一行中都有一個復選框(用於多刪除)。

我現在的問題是,當用戶決定使用SimpleCursorAdapter進行多次刪除時,我是否能夠獲取每個復選框的狀態(無論是否選中)?

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM