我想删除Sqlite数据库中的行。在Listview上长按时打开弹出窗口。(弹出窗口打开ContextMenu)。弹出窗口包括两个选项:"删除","更新"。按下"删除"我想删除该行。
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Action");
menu.add(0, v.getId(), 0, "Delete");
menu.add(0, v.getId(), 0, "Update");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ListView lst=(ListView) findViewById(R.id.ListViewVeriler);
if(item.getTitle()=="Delete"){function1(item.getItemId());}
else if(item.getTitle()=="Update"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
ListView lst=(ListView) findViewById(R.id.ListViewVeriler);
VeriTabani vt=new VeriTabani(MainActivity.this);
//VeriTabani is my database class
int idd=(int)lst.getSelectedItemId();
vt.VeriSil(idd);
//VeriSil is delete method in VeriTabanı class
Toast.makeText(this, "Deleted Succesfully", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Update Person", Toast.LENGTH_SHORT).show();
// I will open UpdateAction here
}
public void VeriSil(int id){
SQLiteDatabase db = this.getWritableDatabase();
try {
db.delete(TABLO_KISILER, ROW_ID_ + " = ?",
new String[] { String.valueOf(id) });
}
catch (Exception e){
}
db.close();
}
答案 0 :(得分:2)
在 ContentProvider
中使用此方法public static void deleteRow(int id)
{
db.execSQL("DELETE FROM "+YOUR_TABLE_NAME+" WHERE id= ?",new Object[]{id});
}
你活动中的调用方法
mContentProvider.deleteRow(id)
额外信息
public void function1(int id){
ListView lst=(ListView) findViewById(R.id.ListViewVeriler);
VeriTabani vt=new VeriTabani(MainActivity.this);
//VeriTabani is my database class
int idd=(int)lst.getSelectedItemId();
mContentProvider.deleteRow(idd);
//VeriSil is delete method in VeriTabanı class
Toast.makeText(this, "Deleted Succesfully", Toast.LENGTH_SHORT).show();
}
全部完成
祝你好运!