I have an adapter and RecyclerView to display the list of contacts saved locally. When the user clicks on an entry, a new activity is launcher where they can edit it. However, I also want to have a "Delete" button at the very bottom to delete the entry, and then go back to the adapter activity. Using the delete query of Room, or otherwise, how can I delete the entry from the EditContact activity?
In ContactDao, I have the following:
// Delete single entry
@Query("DELETE FROM contacts_table WHERE id = :userId")
void deleteByContactId(long userId);
ContactRepository:
public void delete(Contact contact) {
new DeleteContactAsyncTask(contactDao).execute(contact);
}
private static class DeleteContactAsyncTask extends AsyncTask<Contact, Void, Void> {
private ContactDao contactDao;
private DeleteContactAsyncTask(ContactDao contactDao) {
this.contactDao = contactDao;
}
@Override
protected Void doInBackground(Contact... contacts) {
contactDao.delete(contacts[0]);
return null;
}
}
Finally, I want in EditContact to delete the entry on click:
public void deleteContact(View view) {
// Delete the entry based on the ID
}
Hi write a query in your dao file
//get a contact
@Query("select * FROM contacts_table WHERE id = :userId")
Contact getConatctByContactId(long userId);
Once you get the contact id in your edit activity, call ths function to get the contact.
But since your delete function works with id only, you do not need to retrieve to delete from the table
UPDATE
declare the dao in the database class
public abstract class AppDatabase extends RoomDatabase {
public abstract ContactDao contactDao();
}
And call it using your database instance
database.contactPointDao().deleteContact()