In the previous articles, I have explained you how to use GET and POST methods to fetch and create new records in android using retrofit. In this article, I am going to show you how to delete a record using DELETE method using retrofit.

For this tutorial, I am using

Now first add new method to ApiInterface.kt

Next, similarly add a new function to Home Repository as well

Next, we will call this method from our viewmodel class

Now we need to delete a particular post on click of the cancel button. The cancel button is in the HomeAdapter class but we need to call the API from our activity, in this case we need to add a listener in our adapter class which will tell our Main Activity, which post needs to delete. So now add a listener in adapter.

Now in onBindViewHolder we have to set a onclick listener on the cancel button and call this callback.

Now we have to implement this listener in our MainActivity and override the respective callback.

Also, add a remove function to the adapter.

Now, it is done and we can see the deletion working fine in our devices.

For Previous Parts : Part 1 , Part 2

For complete code, go checkout this github repository

Android app developer at ManthonGo.