Kotlin Firestore example – CRUD Operations with RecyclerView | Android

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial show you an Android app that can do Firestore CRUD Operations with RecyclerView.

Related Post: Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android

I. Technologies

– Android Studio 3
– Kotlin 1.2.0
– Firebase Firestore 11.8.0

II. Overview

1. Goal

We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with RecyclerView:

cloud-firestore-crud-demo-show-notes

Firebase Console for Firestore will be like:

cloud-firestore-crud-demo-console-show-notes

2. Cloud Firestore
2.1 Add Firestore to Android App

– Create Android Project, then go to Firebase Console, create Firebase Project:

cloud-firestore-console-add-project

– When your app is created on Firebase Console, you will need to register Android Project. Now fill project package name to register your app:

kotlin-cloud-firestore-add-firebase-project

– Download google-service.json config file and follow instructions:

cloud-firestore-console-add-firebase-android-json

– Add Service plugin for Gradle:

cloud-firestore-console-add-firebase-gradle

– Open build.gradle (App-level), add dependency:

– We also need Authentication to work with Firestore. To simplify this example without Authentication steps, we will allow anyone be able to read or write to the database:

cloud-firestore-console-config-auth

2.2 Initialize & Reference

2.3 Add Data

Assume that our database structure is like:

– using set() to create or overwrite a single document:

– using add(), Cloud Firestore will auto-generate an ID:

2.4 Get Data

– get a Document from Collection:

– get all Documents from Collection:

2.5 Delete Data

2.6 Get Realtime Updates

When no longer listening to data, we must detach listener so that event callbacks stop getting called:

3. Project Structure

kotlin-cloud-firestore-crud-structure

III. Practice

1. Set up Project

– Create New Project with package name com.javasampleapproach.kotlin.firebase.cloudfirestore.
– Add images (found in source code) to drawable.
– Follow these steps to add Firestore to the Project.

2. Layout
2.1 Main Activity

Open res/layout/activity_main.xml file:

2.2 Item Layout

Add item_note.xml layout file to res/layout:

2.3 Activity Layout for Adding/Updating Note

Add activity_note.xml layout file to res/layout:

2.4 Menu

Under res folder, create menu folder and add menu_main.xml:

3. Data Model

Under model package:

4. RecyclerView Adapter Class

To work with RecyclerView, we will:
– create RecyclerView.Adapter subclass
– create ViewHolder class

To support edit Note and delete Note, we register a callback for ivEdit and ivDelete ImageView.

To transfer data between 2 Activity, we use Intent.

Under adapter package:

5. Activity
5.1 Main Activity

To work with RecyclerView, we will:
– set LayoutManager
– set adapter

To work with Add Note button on Menu, we will:
– override onCreateOptionsMenu() function to specify menu layout resource
– override onOptionsItemSelected function to launch NoteActivity

To display List of Items, we will create loadNotesList() and call it inside onCreate().

To update data change, we register a Listener and remove it in onDestroy().

5.2 Note Activity

6. Android Manifest

Define NoteActivity class as an Android Activity:

IV. Source Code

KotlinFirebaseCloudFireStore-CRUD

By grokonez | January 10, 2018.

Last updated on July 13, 2018.



Related Posts


3 thoughts on “Kotlin Firestore example – CRUD Operations with RecyclerView | Android”

  1. I’m getting this error trying to run the project in Android Studio on Mac. Anyone know how to fix

    Error:
    Task ‘assemble’ not found in root project ‘KotlinFirebaseCloudFireStore’.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*