jeudi 23 avril 2015

How to delete selected rows in sqlite android using checkbox

I am having a checkbox beside each row in my custom listview.The user selects the rows he wants to delete.What i want to accomplish is to delete only the selected rows using checkbox.The following is what i have tried:

On my activity:

public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();

if (id == R.id.action_delete_selected) {
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);

            if (cb.isChecked()) {
                myDb.deleteSingleContact(id);
                Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                showTable();
            }

And in my DBHelper:

public void deleteSingleContact(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_ID+"="+id,null);

           }

However the code does not delete the rows and still dispalys the toast.What am i missing?Any help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire