0

无法解决以下问题。我有保存在 SQLite 数据库中的项目列表和这些项目的名称。我使用 onContextItemSelected 方法对列表中的项目进行操作(添加和删除)。到目前为止,向数据库中添加新名称并在列表中反映新项目没有问题;但删除让我发疯。当我单击删除时,项目已从列表中删除,但名称无法从数据库中删除。

这是代码

       if (item.getItemId() == CM_DELETE_ID) {

                AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

                // delete item from list
                bazar.remove(acmi.position);

                // get clicked item id from list                    
                rowID = acmi.id;

                // open database          
                datasource.openToWrite();

                if (cursor != null) {

Log.d(LOG_TAG, "--- Delete from mytable: ---");     

//delete from database
datasource.db.delete(DBHelper.TABLE, DBHelper.COLUMN_ID + " = " + rowID, null);

Log.d(LOG_TAG, "deleted row = " + datasource.db.delete(DBHelper.TABLE, DBHelper.COLUMN_ID + " = " + rowID, null));                                        

                } else {
                    Log.d(LOG_TAG, "Cursor is null");
                    cursor.close();
                }
                // close database
                datasource.close();

                myAdapter.notifyDataSetChanged();

                return true;

日志说

 05:12:08.403   1131    example.CustomAdapter   DEBUG       row inserted, ID = 1
05:12:08.413    1131    example.CustomAdapter   DEBUG       ID = 1, name = A1
05:12:22.853    1131    example.CustomAdapter   DEBUG       row inserted, ID = 2
05:12:22.864    1131    example.CustomAdapter   DEBUG       ID = 1, name = A1
05:12:22.864    1131    example.CustomAdapter   DEBUG       ID = 2, name = A2
05:12:23.623    111 jp.co.omronsoft.openwnn DEBUG   dalvikvm    GC_EXTERNAL_ALLOC freed 20K, 44% free 3471K/6151K, external 3902K/3936K, paused 169ms
05:12:41.833    1131    example.CustomAdapter   DEBUG       --- Delete from mytable: ---
05:12:41.984    1131    example.CustomAdapter   DEBUG       deleted row count = 0

在 onCreate 我有 dbReadAll 方法,列出了我拥有的所有项目和 bazar.add(new Bazar(names, R.drawable.unread)); 是我的数组列表

    void dbReadAll() {

    bazar.add(new Bazar(names, R.drawable.unread));

    // подключаемся к базе данных для чтения данных
    datasource.openToRead();

    // получаем значение курсора        
    cursor = datasource.queryAll();

    if (cursor.moveToFirst()) {

        nameColIndex = cursor.getColumnIndex(DBHelper.COLUMN);

        do {

            Log.d(LOG_TAG, "ID = " + cursor.getInt(idColIndex) + ", name = " + cursor.getString(nameColIndex));

            names = cursor.getString(nameColIndex);

            bazar.add(new Bazar(names, R.drawable.unread));

        } while (cursor.moveToNext());

    } else {
        cursor.close();
        datasource.close();
    }
}
4

2 回答 2

0

我认为您映射行 ID 错误, rowid 可能不等于 acmi.id 。

所以你可以做的是从光标获取行ID

cursor.movetoPosition(acme.id);    
rowid = cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID))
于 2012-10-17T06:15:56.163 回答
0

尝试这个:

//delete from database
datasource.db.delete(DBHelper.TABLE, DBHelper.COLUMN_ID + "=?", new String[]{Long.toString(rowID)});

似乎删除语句使用预先准备好的查询有关更多信息,请参见这个软问题

UPDATE更改rowIdLong.toString(rowId)我们需要在字符串数组中输入一个字符串

于 2012-10-17T05:28:54.523 回答