通过从填充的ListView中读取ID,从SQLite数据库中删除一行

时间:2014-04-07 20:42:57

标签: android database delete-row corresponding-records

如标题中所述,我必须通过单击ListView项目填充按钮,删除我想要的每一行,通过调用项目ID并使用它编译函数。

我的删除功能就是这个:

public boolean deleteBook(long bookID) {
return database.delete(DATABASE_TABLE, KEY_BOOKID + "=" + bookID, null) > 0;
}

我想在这个函数中使用它:

Button delete_btn = (Button) findViewById(R.id.delete_btn);
delete_btn.setOnClickListener(
    new View.OnClickListener() {
        public void onClick(View aView) {
            // NEEDED CODE!
        }
    }
);

我填充的ListView由以下规则完成:

// Display Database values
ListView BookList = (ListView) findViewById(R.id.listView1);
DbAdapter db = new DbAdapter(getApplicationContext());
db.open();
Cursor cursor = db.fetchAllBooks();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.book, cursor, new String[]{ DbAdapter.KEY_TITLE, DbAdapter.KEY_AUTHOR }, new int[]{ R.id.booktitle, R.id.bookauthor });
BookList.setAdapter(adapter);
if (cursor.moveToFirst()) {
    while (cursor.moveToNext());
}
db.close();

我的DbAdapter就是这个:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DbAdapter {
@SuppressWarnings("unused")
private static final String LOG_TAG = DbAdapter.class.getSimpleName();

private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;

// Database fields
private static final String DATABASE_TABLE = "book";

public static final String KEY_BOOKID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_AUTHOR = "author";

public DbAdapter(Context context) {
this.context = context;
}

public DbAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}

public void close() {
dbHelper.close();
}

private ContentValues createContentValues(String title, String author ) {
ContentValues values = new ContentValues();
values.put( KEY_TITLE, title );
values.put( KEY_AUTHOR, author ); 
return values;
}

//create a book
public long createBook(String title, String author ) {
ContentValues initialValues = createContentValues(title, author);
return database.insertOrThrow(DATABASE_TABLE, null, initialValues);
}

//update a book
public boolean updateBook( long bookID, String title, String author ) {
ContentValues updateValues = createContentValues(title, author);
return database.update(DATABASE_TABLE, updateValues, KEY_BOOKID + "=" + bookID, null) > 0;
}

//delete a book      
public boolean deleteBook(long bookID) {
return database.delete(DATABASE_TABLE, KEY_BOOKID + "=" + bookID, null) > 0;
}

//fetch all books
public Cursor fetchAllBooks() {
return database.query(DATABASE_TABLE, new String[] { KEY_BOOKID, KEY_TITLE, KEY_AUTHOR }, null, null, null, null, null);
}

//fetch books filter by a string
public Cursor fetchBooksByFilter(String filter) {
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                                KEY_BOOKID, KEY_TITLE, KEY_AUTHOR },
                                KEY_TITLE + " like '%"+ filter + "%'", null, null, null, null, null);

return mCursor;
}
}

1 个答案:

答案 0 :(得分:0)

试试:

db.deleteBook(adapter.getItemId( /* get your id here */ ));