0

I've added data into my SQLite database, and it shows inside listView. But I want to delete it with Context menu. Context menu is showing, but data is not deleted.

Here is sample of my code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    mDataBase = dbHelper.getWritableDatabase();

    switch(item.getItemId()) {
        case R.id.delete:

            mDataBase.delete("orderTable", "_id" + " =? ", new String[] {String.valueOf(info.id)});

            dbAdaper.setArrayMyData(selectAll());
            dbAdaper.notifyDataSetChanged();
            mDataBase.close();

    }
    return super.onContextItemSelected(item);
}

I found out that info.id has 0 value.

 dbAdaper = new DBBasketAdapter(getActivity(), selectAll());
    listView.setAdapter(dbAdaper);
    registerForContextMenu(listView);

1 Answer 1

1

The code converts the ID into a string.

For integers, using SQL parameters is not necessary; just use the value directly:

mDataBase.delete("orderTable", "_id = " + info.id, null);
Sign up to request clarification or add additional context in comments.

1 Comment

I hate passing values directly in statements!!! But I must aggree with you, as workaround for handling String [] whereArgs is worthless...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.