简体   繁体   中英

Android db.delete(); on ListActivity

I need help on a matter...

I created a listview with a cursord adapter that connects to my database,and shows me a list of everything that I saved in my table. I later created a context menu that appears with longckick on the desired line

context position

         id = getListAdapter().getItemId(info.position);

this is my method delete in the databaseHelper

void deleteReg(SQLiteDatabase db)
    {

        db.delete(TabellaRegistry.TABLE_NAME,null, null);
    }

and i use that in my activity for delete the selected line:

   final SQLiteDatabase db = databaseHelper.getWritableDatabase();
                                databaseHelper.deleteReg(db,null, null, null, null, null, null);

but doing so...i delete all in my table...

how do I delete only the selected line via the context menu?

I hope I explained myself, thanks in advance

By passing NULL as your second argument on this line:

db.delete(TabellaRegistry.TABLE_NAME,null, null);

You will always be deleting the whole table. As mentioned in the Android documentation, the parameters are as following:

public int delete (String table, String whereClause, String[] whereArgs)

table - the table to delete from

whereClause - the optional WHERE clause to apply when deleting. Passing null will delete all rows.

So let's say you want to delete the line where BOOK_ID is 6 you would have something like:

public boolean deleteTitle(String id) 
{
    return db.delete(DATABASE_TABLE, BOOK_ID + "=" + id, null) > 0;
}

SQLite Class Overview: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

为了删除特定的行,您需要在delete语句delete中放置一个where子句

If you check the api docs for this method you can get the answer yourself.

public int delete (String table, String whereClause, String[] whereArgs)

Parameters

table   the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.

You are passing null in where clause which is the cause of deleting all records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM