DEV Community

loizenai
loizenai

Posted on

Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android

https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-firebaseui-firestorerecycleradapter-android

Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | 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 Android RecyclerView and FirebaseUI FirestoreRecyclerAdapter.

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

I. Technologies

  • Android Studio 3
  • Kotlin 1.2.0
  • Firebase Firestore 11.8.0
  • FirebaseUI Firestore 3.1.0

    II. Overview

    1. Goal

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

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

Follow these steps to add Firestore to the Project.

2.2 Initialize & Reference


// Access a Cloud Firestore instance from your Activity
val db = FirebaseFirestore.getInstance()
 
// Reference to a Collection
val notesCollectionRef = db.collection("notes")
 
// Reference to a Document in a Collection
val jsaDocumentRef = db.collection("notes").document("jsa")
// or
val jsaDocumentRef = db.document("notes/jsa")
 
// Hierarchical Data with Subcollection-Document in a Document
val androidTutRef = db
        .collection("notes").document("jsa")
        .collection("tutorials").document("androidTutRef")

2.3 Add/Update/Get/Delete Data & Get Realtime Updates

Visit this part from previous Post for details.

3. FirebaseUI Firestore

To use the FirebaseUI Firestore to display list of data, we need:

  • Java class for data object (Model)
  • Java class for holding UI elements that match with Model's fields (ViewHolder and layout)
  • Custom RecyclerView adapter to map from a collection from Firestore to Android (FirestoreRecyclerAdapter)
  • RecyclerView object to set the adapter to provide child views on demand.

cloud-firestore-ui-crud-demo-show-notes

More at:

https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-firebaseui-firestorerecycleradapter-android

Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android

Top comments (0)