【发布时间】:2012-11-15 10:26:15
【问题描述】:
我想从列表视图中删除特定行,其中数据是从数据库中填充的。在setOnItemLongClickListener 上,我已经能够从列表视图中删除数据,但是当我重新启动我的活动时,我可以看到我已删除的行在那里,因为在onCreate() 我从数据库中获取数据。
我做了什么:
MainActivity.java
db = new AndroidOpenDbHelper(this);
showing_history
.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, final int pos, final long id) {
final AlertDialog.Builder b = new AlertDialog.Builder(
MainActivity.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Delete this from history?");
b.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
HistoryNamesList.remove(pos);
((BaseAdapter) mHistoryListAdapter)
.notifyDataSetChanged();
del(pos);
}
});
b.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
});
b.show();
return true;
}
});
我在 MainActivity.java 中的删除方法:
private void del(int j) {
if (db.deleteTitle(j))
Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(this, "Delete failed.", Toast.LENGTH_LONG).show();
db.close();
}
我在 AndroidOpenDbHelper.java 中的删除查询
public boolean deleteTitle(long rowId) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME_HISTORY, ID + "=" + rowId, null) > 0;
}
同样,我可以从列表视图中删除数据,但不能从数据库中删除。请帮我解决这个问题。
【问题讨论】:
-
任何,比如,错误,堆栈跟踪,日志?
-
你可能想看看 ContentProviders。发布您的 onCreate 方法。
-
位置不是 id。还有,什么是 deleteTitle ?
-
代替del(pos) 使用del(id);
-
完全没有错误,只是吐司说删除失败。 del(id),无助于清理数据库数据。
标签: android sqlite android-listview android-sqlite onlongclicklistener