Deleting a single SQLite row

2019-09-13 06:08发布

I am trying to delete a single row using onLongClick of list item but sometime it deletes the row and sometimes doesn't. Maybe I am passing the wrong id to it I don't getting what actually problem is.

I have tried some code and I am trying from last 2 days but unable to resolve this.

Below is my code.

@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;
    }

And 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();
        }
    }

I am not getting any error, though, but it is not deleting item from database or from listview

1条回答
贼婆χ
2楼-- · 2019-09-13 06:48

First make a id variable in your model class and generate getter and setter for that:

public static final String PRIMARY_ID="_id";

Now set the primeary_id to model from the cursor:

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());
        }

Now for deleting that row use this primary key:

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

The delete function is exactlu same as yours, as I mentioned in comment that the problem is with id not your delete function:

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();
    }
}
查看更多
登录 后发表回答