Press ESC to close

Android SQLite CRUD – RecyclerView ContextMenu- INSERT SELECT, DELETE

This is an android tutorial of how to work with SQLite datbase and RecyclerView with ContextMenu.

Today we see how to insert data to sqlite database from an input dialog,select that data and show it in a RecyclerView. We shall also see how to delete.Now for deleting we shall use a ContextMenu. User longclicks/long presses a RecyclerView card,then selects the action to perform. In short :

We perform the following:

Insert Data to SQLite Database

Retrieve Data from SQLite Database and render in a RecyclerView

Long press the recyclerview to show a contextmenu.

When uses longpresses,he gets presented with a ContextMenu to select the action to perform.

If he selects “New” we show an input dialog.While if he selects “Delete”,we delete the data from our database.

Our RecyclerView classes

(a). MyLongClickListener.java

Our RecyclerView viewitems long click listener interface. With contextmenu we simply mean you longclick and you are then presented with a menu where you can select a menu item.Therefore we need to implement the OnLongClickListener.But first lets define its signature :

(b). MyHolder.java

Our RecyclerView.ViewHolder class.The above LongClickListener interface shall get implemented by our ViewHolder class.Take note that also we implement the OnCreateContextMenuListener interface as well :

(c). MyAdapter.java

Our RecyclerView Adapter class.Remember we shall be using using a RecyclerView as our component,therefore here’s our adapter class. It Will inflate our model.xml layout into a view objects and bind data from the sqlite database to them.

Download

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