RecyclerView with sqlite database android tutorial

The following gif is an overview of what we are about to build
add, edit, delete, search contacts



First of all create a new project in android studio
now setup the following dependencies in your app level build.gradle file

implementation 'com.android.support:appcompat-v7:28.0.0' 
implementation 'com.android.support:design:28.0.0' 
implementation 'com.android.support:recyclerview-v7:28.0.0' 
implementation 'com.android.support:cardview-v7:28.0.0'

Now setup all the assets like shown below

go to res >> drawable and create brown_border.xml,pedit.png,pdelete.png

brown_border.xml

pedit.png(download)


pdelete.png(download)


Now go to res >> values >> colors.xml and add the following

Now go to res >> values >> strings.xml and add the following

Now create res >> menu >> menu_main.xml like this

Now create res >> layout and add the following

activity_main.xml add_contact_layout.xml contact_list_layout.xml
Now let us look into some java code
First of all create a helper class which extends SQLiteOpenHelper as shown below

SqliteDatabase.java
The above helper class provides methods to perform all the basic CRUD operations like add, update, delete etc.Now we should create a model class for the contacts object, it should be like

Contacts.java
Now we should create a recyclerview adapter for our recyclerview. It will include a Filter method which would be used to do search operations within our contacts list.

ContactAdapter.java

Below is the code for ContactViewHolder.java which is used in the above adapter class
Update your MainActivity.java file like shown below
With this, we have come to the end of this tutorial, now try running the app!

2 comments: