Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android
Tags: kotlin, android, firestorerecycleradapter, firebaseui
Published: 2021-05-22 06:17:48

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 <code>RecyclerView</code> and FirebaseUI <code>FirestoreRecyclerAdapter</code>.

Related Post: <a href="https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-recyclerview-android">Kotlin Firestore example – CRUD Operations with RecyclerView | Android</a>

<!--more-->
<h2>I. Technologies</h2>
- Android Studio 3
- Kotlin 1.2.0
- Firebase Firestore 11.8.0
- FirebaseUI Firestore 3.1.0
<h2>II. Overview</h2>
<h3>1. Goal</h3>
We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with  with Android <code>RecyclerView</code> and FirebaseUI <code>FirestoreRecyclerAdapter</code>:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-crud-demo-show-notes.png" alt="cloud-firestore-crud-demo-show-notes" width="657" height="514" class="alignnone size-full wp-image-10246" />

Firebase Console for Firestore will be like:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-crud-demo-console-show-notes.png" alt="cloud-firestore-crud-demo-console-show-notes" width="700" height="230" class="alignnone size-full wp-image-10250" />
<h3>2. Cloud Firestore</h3>
<h4>2.1 Add Firestore to Android App</h4>
Follow <a href="https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-recyclerview-android#21_Add_Firestore_to_Android_App">these steps</a> to add Firestore to the Project.
<h4>2.2 Initialize & Reference</h4>
<pre><code class="language-kotlin">
// 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")
</code></pre>
<h4>2.3 Add/Update/Get/Delete Data & Get Realtime Updates</h4>
Visit <a href="https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-recyclerview-android#23_Add_Data">this part from previous Post</a> for details.
<h3>3. FirebaseUI Firestore</h3>
To use the FirebaseUI Firestore to display list of data, we need:
- Java class for data object (<strong>Model</strong>)
- Java class for holding UI elements that match with Model's fields (<strong>ViewHolder</strong> and layout)
- Custom RecyclerView adapter to map from a collection from Firestore to Android (<strong>FirestoreRecyclerAdapter</strong>)
- <strong>RecyclerView</strong> object to set the adapter to provide child views on demand.

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-ui-crud-demo-show-notes.png" alt="cloud-firestore-ui-crud-demo-show-notes" width="325" height="514" class="alignnone size-full wp-image-10280" />

More at:

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

Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android