我想要的是当用户clic联系上下文菜单时显示项目“删除”所以我这样做了,但问题是我想要当他从Cqlit删除一行时(这对应于联系我)刚点击) Activity.class
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int i = info.position;
switch (item.getItemId()) {
case R.id.delete:
dbHandler.deleteEtudiant(i);
Toast.makeText(this,"deleted "+i, Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
MyDBHandler.class
public void deleteEtudiant(int i)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete("etudiant", "Id+ =" + i, null);
}
答案 0 :(得分:0)
您的代码的问题是您获取“AdapterContextMenuInfo”对象的id并将其发送到数据库“delete()”方法。但实际上你需要发送数据库项的id,该值存储在SQlite数据库中该特定行的“ID”列中。
请查看下面给出的教程,特别检查本教程的“getContact()”和“deleteContact()”方法。
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/