Press ESC to close

Android SQLite – RecyclerView – CRUD then Swipe To Delete Data

We want to see how we can implement a ItemTouchHelper with a RecyclerView filled from SQLite database.

Thus if the user swipes away a RecyclerView item, that item is deleted automatically from the database.

But first we see how to insert into mysql database , then retrieve that data and show in a RecyclerView. Then user can delete by swiping away.

Our mission at ProgrammingWizards Youtube Channel is to provide practical realworld examples.There is alot of theory on the web.But then its always easier to learn by doing.So we aim to fill that gap by providing quality examples,that you can easily extend and use in your app. Today we talk about Swipe To Delete,of course from Swipe to Dismiss.We use ItemTouchHelper class,no third party library.We shall do these:

INSERT,SELECT and DELETE to and from SQLite database.

To delete we shall use the Swipe to dismiss technique,in this case swipe to delete from database.

We then refresh our adapter for instant change notifications.

1. Create Basic Activity Project

Let’s start by creating a basic project in android studio. You can see how to do so here.

Our ItemTouchHelper Class

(a). SwipeHelper.java

This is our ItemTouchHelper.SimpleCallback class.

We are creating a swipe helper class that’s going to derive from ItemTouchHelper.SimpleCallback.Its going to take our Adapter reference and invoke the method for deleting data inside the onSwiped method :

This class will allow us delete a planet from our RecyclerView when user swipes that item in the RecyclerView.

(b). MyAdapter.java

Our RecyclerView Adapter class. It has to extend the RecyclerView.Adapter<MyHolder> class. The MyHolder generic parameter is our Recyclreview ViewHolder class.

Our RecyclerView shall have this adapter class so that it binds our data to inflated views. That inflation will also take place here.

Our constructor will take a context and an arraylist of planet objects. Those planet objects are what will populate our Recyclerview. The Context object shall be needed for inflation of the model.xml layout to occur.

Our Activity Class

(a). MainActivity.java

Our MainActivity class.
This class represents our user interface. This is where we will render our user interface widgets like RecyclerView. We will also be able to show an input dialog. That is the dialog used to type data that should be saved in sqlite database. So that dialog also in fact gets hosted by this activity.

Share Article:

Tags:

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com .
Follow me below;
Github
, and on my channel:
ProgrammingWizards TV