java - Android Sqlite - 获取正确的数据库行id

标签 java android sqlite android-listview

我正在开发一款应用程序,允许用户在排练戏剧时创建笔记。用户可以在 ListView 中查看他们创建的注释,并根据需要编辑和删除它们。

以用户创建 3 条笔记为例。在数据库中,row_id 将为 1、2 和 3。因此,当用户在 ListView 中查看注释时,它们也将按 1、2、3 的顺序排列(在我增加值之前最初为 0、1、2) 。这样用户就可以从数据库中查看和删除正确的行。

当用户决定删除注释时就会出现问题。假设用户删除了位置 2 中的注释。因此我们的数据库将具有 row_id 的 1 和 3。但是在 ListView 中,它们将位于位置 1 和 2。因此,如果用户单击 ListView 中位置 2 中的注释,应该返回数据库中 row_id 3 的行。但是它尝试查找不存在的 row_id 2,因此崩溃。

我需要知道如何根据用户在 ListView 中的选择来获取相应的 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()”方法。

这是旧的实现,带有我自己的自定义适配器:

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 进行多次删除时,我是否能够获取每个复选框的状态(无论是否选中)?

谢谢。

关于java - Android Sqlite - 获取正确的数据库行id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13771809/

相关文章:

JavaFX - 图像、imageview 和

java - Craftbukkit : mvn clean install error

java - 检查 Java 方法有多少个参数

java - 如何在 Android 中从网络获取时间

sqlite - 有没有办法在SQLite数据库上运行SQL报告?

java - 在java中通过套接字连续流式传输数据

android - 如何在 flutter 中使用for循环?

java - 如何对多种语言进行排序?

c# - 将值从 SQL 返回到 WPF 表单 - 我做得正确吗?

android - 如何在应用程序中存储大数据?