Deleting a single SQLite row

岁酱吖の 提交于 2020-01-06 15:13:41

问题


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:


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


来源:https://stackoverflow.com/questions/39474572/deleting-a-single-sqlite-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!