【发布时间】:2014-05-20 07:15:52
【问题描述】:
正如标题中所写,我必须通过单击 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;
}
}
【问题讨论】:
-
如何填充列表视图?使用游标加载器而不是已弃用的 startManagingCursor() 方法。此外,最好将您的数据库操作放在后台线程上 - 加载器将使其变得容易。您需要列表视图项和 ID 号之间的简单关联。在您的列表视图适配器中处理此问题。
-
@CollinFlynn :我正在填充我在第一篇文章中所写的内容。我应该改变什么?我是 Android 应用开发新手...
标签: android database delete-row corresponding-records