I am new to android development and I have populated values to a listview using a cursor adapter. I am trying to Delete and update the values using the list view but I am not sure how this is done using a cursor adapter. Nor I am able to click on the list view item
The below methods I have used in my database handler class to delete and update values
Delete Method
public void DeletingCustodian(Custodians custodians)
{
SQLiteDatabase db_database = getWritableDatabase();
//Deleting the custodian from the Database where the custodian ID matches to the selcted ID
db_database.delete(TABLE_CUSTODIAN,CUSTODIAN_ID + "=?" , new String[]{String.valueOf(custodians.getCust_id())});
db_database.close();
}
Updating Method
public int updateCustodian(Custodians cust)
{
SQLiteDatabase db_database = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CUSTODIAN_NAME,cust.getCust_Name());
values.put(CUSTODIAN_DESIGNATION,cust.getCust_Design());
values.put(CUSTODIAN_DEPARTMENT,cust.getDepartment());
int roweffected = db_database.update(TABLE_CUSTODIAN,values,CUSTODIAN_ID + "=?", new String[]{String.valueOf(cust.getCust_id())});
db_database.close();
return roweffected;
}
I have created a context Menu which displays edit and delete this is shown when a certain item is selected.
public void onCreateContxtManu(ContextMenu menu,View view, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu,view,menuInfo);
menu.setHeaderTitle("Custodian Options");
menu.add(Menu.NONE,EDIT,menu.NONE,"Edit Custodian");
menu.add(Menu.NONE,DELETE,menu.NONE,"Delete Custodian");
}
public void deletingitemsfromlist()
{
CustodianListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return false;
}
});
}
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case EDIT:
break;
case DELETE:
break;
}
return false;
}
try this , It may help you
Delete code in Database Connector
in Your Listview page code Onitem long click Delete
Update code
Update Database code