Android
Data objects

Basic Operations

13min

CRUD Parse objects in Android

Introduction

In this section we are gonna build an Android application that performs basic CRUD operations. CRUD is abbreviation of Create-Read-Update-Delete. When you store data on Parse it is built around ParseObject and each one contains key-value pairs of JSON-compatible data.

The values you can store in Back4App Database could be in type of String, Number, Bool, Array, Object, Date, File, Pointer, Relation and null.

You can get more information about data types by clicking here.

This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion 30

At any time, you can access the complete Project via our GitHub repositories.

Goal

Here is a preview of what we are gonna achive:

Document image


Prerequisites

To complete this tutorial, we need:

Understanding our Todo App

To better understand Parse on Android, you will see the CRUD operations implemented on a ToDo App. The application will have a simple interface, with a title and description text field to register a task and a list of registered tasks. You can update each task’s title or description.

Note:

  • In this tutorial we will make our own custom alert dialog. Therefore, the codes will be configured to follow the template. You can adjust it to your own design.
  • If you have any trouble, check the GitHub repositories for Kotlin and Java.

Let’s get started!

Following the next steps, you will be able to build a Todo App that will store the tasks at Back4App Database.

1 - Create Todo App Template

Go to Android Studio, and find activity_main.xml layout file from the Project (Project/app/res/layout/activity_main.xml) and then replace the code below with your own code. This xml code will be our MainActivity's design, and we will bind this views to our MainActivity.java class and use them.

XML


2 - Create Object

The create function will create a new Task with the title and description.

To add a TODO, we enter the title and description values in the pop-up that appears on the screen, set it to the ParseObject and save this object. We save this object in the database by using the function that Parse has provided us.

Note:

  • In this project we will make our own custom alert dialog. You can design your Alert dialog as you want and set it to the view of the dialog you will use later.
Java
Kotlin


3 - Read Object

With the function that Parse has provided us, we can fetch all the data in a class as ParseObject list. We create a query like the one below and fetch all the data in the Todo class.

Java
Kotlin


In this function, we give the returned list as a parameter to our adapter, set this adapter, to our RecyclerView, and we print the values of each object of this list into its own views. And if list has no item, we are setting our empty_text, view’s as VISIBLE.

Java
Kotlin


4 - Update Object

With the edit button in the view of our adapter, we are performing our updates. With the MutableLivedata, object provided by Android, we can listen to the click event of the edit button on our home page. When this button is clicked, we open the same pop-up and this time we populate this pop up with the title and description values of the object we clicked. Then, when the user changes this description and title and press save, first we fetch from the database with the id of this object, then we set the new variables and save object again.

Note:

  • MutableLiveData is a subclass of LiveData which is used for some of it’s properties (setValue/postValue) and using these properties we can easily notify the ui.
Java
Kotlin


5 - Delete Object

We listen to the click event of the delete button in the view of our adapter with the MutableLivedata object from the home page, as in the edit button. When the delete button is clicked, we give the object id of the ParseObject as a parameter to the delete function that Parse has provided us and delete this object from the database.

Java
Kotlin


It’s done!

At this point, you have learned how to do the basic CRUD operations with Parse on Android.