我需要更改onListItemClick()方法以删除列表中的项目..想法? 我不知道如何让它适应我的课程。我有一个Db类和一个Helper类。
以下是代码:
Cursor cursor = db.rawQuery("select * from " + PRODUCT_TABLE, null);
setListAdapter(new CursorAdapter(this, cursor, true) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
TextView textView = new TextView(ProdottiSelectionActivity.this);
return textView;
}
@Override
public void bindView (View view, Context context, Cursor cursor){
TextView textView = (TextView) view;
textView.append(cursor.getString(1));
textView.append("\n");
textView.append(cursor.getString(2));
textView.append("\n");
textView.append(cursor.getString(3));
textView.append("\n");
textView.append(cursor.getString(4));
}
});
}
@Override
protected void onDestroy(){
super.onDestroy();
dbHelper.close();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
setResult(1, new Intent(String.valueOf(id)));
finish();
}
答案 0 :(得分:1)
您将需要以下内容
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
db.open();
//you have to determine user clicked on which item
String[] whereArgs={String.valueOf(item_id)};
db.delete(DATABASE_TABLE_4,"item_id = ?",whereArgs);
cursor.requery(); //deprecated
adapter.notifyDataSetChanged(); //deprecated
db.close();
}
答案 1 :(得分:0)
在数据库上运行删除查询,这也会更新列表视图。
db.execSQL("delete from " + PRODUCT_TABLE + " where id = 1", null);
例如,如果您要删除ID为1的产品,或者您也可以使用delete method。
另见问题
与您的问题相似。