EmployeeDao.java
1/ Add DAO functions to delete a single employee by id
2/ Add DAO function to delete all employees
// SQL to delete everything?
@Query("DELETE FROM employees")
public void deleteAll();
// SQL to delete by id?
@Query("DELETE FROM employees WHERE id = :empId")
public void deleteEmployeeById(int empId);
Main Activity:
1/ Call the DAO functions in your button click handlers
public void deleteByIdPressed(View view) {
// 1. Get the id from ther userinterface
EditText etId = findViewById(R.id.etId);
if (etId.getText().toString().isEmpty()) {
Log.d("ABC", "Sorry, you must provide an id");
return;
}
// 2. Call the corresponding delete function
int id = Integer.parseInt(etId.getText().toString());
empDAO.deleteEmployeeById(id);
Log.d("ABC", "Delete finished, check database to confirm it worked.");
}
public void deleteAllPressed(View view) {
empDAO.deleteAll();
Log.d("ABC", "All employees deleted");
}
Share with your friends: |