我想在没有 cursor.requery 的情况下使用该行中的按钮删除列表视图中的一行,因为它已贬值
I want to delete a row in a listview using a button in that row without cursor.requery as it is depreciated
我正在使用带有 3 个 TextView 和 1 个按钮的 CursorAdapter。我想在按下按钮时从我的数据库中删除该条目。
注意:有很多关于此的帖子,但是它们都使用现在已贬值的 "cursor.requery"
建议相同的代码
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView viewID = (TextView) view.findViewById(R.id.viewID);
TextView viewAmount=(TextView) view.findViewById(R.id.viewAmount);
TextView viewCategory=(TextView) view.findViewById(R.id.viewCategory);
Button buttonDelete = (Button) view.findViewById(R.id.buttonDelete);
final int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String amount = cursor.getString(cursor.getColumnIndexOrThrow("amount"));
String category = cursor.getString(cursor.getColumnIndexOrThrow("category"));
viewID.setText(String.valueOf(id));
viewAmount.setText(amount);
viewCategory.setText(category);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted item " + id, Toast.LENGTH_SHORT).show();
//Do I create a DB handler here and run a dbHandler.deleteItem(id)
//method or do something else?
}
});
}
试试这个
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted item " + id, Toast.LENGTH_SHORT).show();
String query = "DELETE * from *table_name* where _id = "+ id + "";
db.execQuery(query) // db is your DatabaseHelper
startActivity(getIntent());
finish();
}
});
}
我正在使用带有 3 个 TextView 和 1 个按钮的 CursorAdapter。我想在按下按钮时从我的数据库中删除该条目。
注意:有很多关于此的帖子,但是它们都使用现在已贬值的 "cursor.requery"
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView viewID = (TextView) view.findViewById(R.id.viewID);
TextView viewAmount=(TextView) view.findViewById(R.id.viewAmount);
TextView viewCategory=(TextView) view.findViewById(R.id.viewCategory);
Button buttonDelete = (Button) view.findViewById(R.id.buttonDelete);
final int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String amount = cursor.getString(cursor.getColumnIndexOrThrow("amount"));
String category = cursor.getString(cursor.getColumnIndexOrThrow("category"));
viewID.setText(String.valueOf(id));
viewAmount.setText(amount);
viewCategory.setText(category);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted item " + id, Toast.LENGTH_SHORT).show();
//Do I create a DB handler here and run a dbHandler.deleteItem(id)
//method or do something else?
}
});
}
试试这个
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted item " + id, Toast.LENGTH_SHORT).show();
String query = "DELETE * from *table_name* where _id = "+ id + "";
db.execQuery(query) // db is your DatabaseHelper
startActivity(getIntent());
finish();
}
});
}