我有一个listView并显示数据库中的数据。我想点击每个项目进行简单的编辑,但我的编辑功能不起作用! 这是我的编辑功能:(数据库是SQLiteDatabase类的对象)
public Book editBook(Book book, String name, String writer, String year) {
long id = book.getId();
String selection = id + " = ?";
String[] selectionArgs = { String.valueOf(id) };
ContentValues values = new ContentValues();
values.put(sqlHelper.COLUMN_NAME, name);
values.put(sqlHelper.COLUMN_WRITER, writer);
values.put(sqlHelper.COLUMN_YEAR, year);
int returnId = database.update(sqlHelper.TABLE, values,selection,selectionArgs);
return book;
}
在MainActivity中调用的编辑函数如下:
ListView list = getListView();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Book data = (Book) parent.getItemAtPosition(position);
datasource.editBook(data,data.getName() + " 2014", data.getWriter() + "," + data.getWriter(), data.getYear());
adapter.notifyDataSetChanged();
}
});
ListView list = getListView();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Book data = (Book) parent.getItemAtPosition(position);
datasource.editBook(data,data.getName() + " 2014", data.getWriter() + "," + data.getWriter(), data.getYear());
adapter.notifyDataSetChanged();
}
});