删除单个SQLite行

时间:2016-09-13 16:09:10

标签: android sqlite sql-delete delete-row

我正在尝试使用列表项的onLongClick删除单行,但有时会删除该行,有时则不删除。也许我把错误的id传递给它我没有得到实际问题。

我尝试了一些代码,我在过去2天内尝试但无法解决此问题。

以下是我的代码。

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
        AlertDialog.Builder alert = new AlertDialog.Builder(FavouriteListActivity.this);
        alert.setMessage("Do you want to unfavorite selected item ?");
        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i("===========", "Position === " + position);
                Log.i("===========", "long id === " + id);
                deleteMethod(id);
                showFavouriteList();
                dialog.cancel();
            }
        });
        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alert.show();
        return true;
    }

和deleteMethod

private void deleteMethod(long Id) {
        try {
            long shId = Id + 1;
            SQLiteDatabase db = OpenHelper.getDataHelper(getApplicationContext());
            String query = "DELETE FROM " + LocalDatabase.FAVORITE_LIST.TABLE_FAVOURITE_LIST + " WHERE " + LocalDatabase.FAVORITE_LIST.FAVOURITE_LIST_ID + " = " + shId;
            Log.i("QUERY", "" + query);
            db.execSQL(query);
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我没有收到任何错误,但它不是从数据库或listview中删除项目

1 个答案:

答案 0 :(得分:0)

首先在模型类中创建一个id变量,然后为其生成getter和setter:

public static final String PRIMARY_ID="_id";

现在将primeary_id设置为光标模型:

if (cursor.moveToFirst()) {
            do {
                model = new FavouriteListVo(lngID, strName);
                  model.setStrName(cursor.getString(cursor.getColumnIndex(LocalDatabase.FAVORITE_LIST.FAVOURITE_LIST_NAME)));
model.setPrimId(cursor.getString(cursor.getColumnIndex(LocalDatabase.FAVORITE_LIST.PRIMARY_ID)));
                arrayListFav.add(model);
            } while (cursor.moveToNext());
        }

现在删除该行使用此主键:

 public void onClick(DialogInterface dialog, int which) {
            String id=model.getPrimId();
            deleteMethod(id);
            showFavouriteList();
            dialog.cancel();
        }

删除功能与你的完全相同,正如我在评论中提到的那样,问题是id不是你的删除功能:

private void deleteMethod(long Id) {
    try {
        long shId = Id + 1;
        SQLiteDatabase db = OpenHelper.getDataHelper(getApplicationContext());
        String query = "DELETE FROM " + LocalDatabase.FAVORITE_LIST.TABLE_FAVOURITE_LIST + " WHERE " + LocalDatabase.FAVORITE_LIST.FAVOURITE_LIST_ID + " = " + shId;
        Log.i("QUERY", "" + query);
        db.execSQL(query);
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}