Cloud Firestore Android example – CRUD Operations with RecyclerView

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 shows you an Android app that can do Firestore CRUD Operations with RecyclerView.

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

I. Technologies

– Android Studio 3
– 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:


Firebase Console for Firestore will be like:


2. Cloud Firestore
2.1 Add Firestore to Android App

– Create Android Project, then go to Firebase Console, create Firebase 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:


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


– Add Service plugin for 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:


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


III. Practice

1. Set up Project

– Create New Project with package name com.javasampleapproach.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


By grokonez | January 9, 2018.

Last updated on July 31, 2018.

Related Posts

7 thoughts on “Cloud Firestore Android example – CRUD Operations with RecyclerView”

  1. Hi,
    Realtime Database seems to work offline just fine, but I notice many people say Firestore’s offline capabilities exceed that of the Realtime Database. How do you think its better?

    1. Hi Eric,

      Both have mobile-first, realtime SDKs and support local data storage for offline-ready apps.
      But Firebase Realtime Database gives offline support for mobile clients on iOS and Android only, and Firestore supports iOS, Android, web clients.


  2. I m using this example when I receive double vale from firestore it shows an error double cant converted in object. I m getting error at :
    Note note = doc.toObject(Note.class)….

    1. Hi Swapnil,

      Make sure that you import the right namespace for Note model class.
      Please give me more details about your problem.


      1. this is my model class

        and i got the error here:

        And in firestore Amount = 157.56 (String) and here i m also set number data type then i got null value for amount

  3. What is the purpose of the codes below
    in the main activity bearing in mind that we have method

Got Something To Say:

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