CREATE – CRUD Using Objectbox

CREATE – CRUD Using Objectbox

Hi guys, Its been quiet some time since I wrote the last article. I come with some good news, Objectbox Version 1 is now out! You can refer to my previous article which I have updated with the latest instructions on how to set up the latest and stable release. I would advise you to check it first, before going on with this article. Lets get started!

CREATE – CRUD Using Objectbox, what does this mean in CRUD? Whenever we want to add a new record to a database we perform a CREATE operation. This ensures our data is created and is persisted so that we can access it later if we need to. Remember I explained that we will be building a Simple Note App, that will allow us to create and save Notes. So how do we save this Notes? We will store them locally using Objectbox, so that we can refer to them later on after we have saved them.

Lets start by opening our existing Project, which we added Objectbox and initialized  Objectbox, or create a new project and use the two previous tutorials to set up your development environment.

Create User Interface

Before we start creating and saving Notes, we need to create a User Interface for our App. A User Interface is what the User sees when they use our Application. We will create a very simple User Interface to start with, so that we spend less time developing it and to make newbies understand it easily.

We will use an EditText and a Button inside a LinearLayout, but we will cover advanced Layouts and UI design concepts later, after we are done with the Objectbox series. Open your activity_main.xml file and replace with the following code.

Change the styles.xml file also under res -> values folder to this (OPTIONAL)

The final app should look like this:

We have managed to create a simple and beautiful user interface, lets move ahead and start handling the “backend”, where we will handle the application logic.

Setting Up

We need to set up a couple of things first. We first declare and initialize both our note, string and objectbox. Add the following code before the onCreate method.

We then initialize objectbox inside the onCreate method using the following code snippet:

We then set onClick method to our button, so that it is called whenever we need to save a new Note. We are using kotlin  anko android extension plugin, so we don’t need Butterknife or findViewById methods, common in Java.

We call  the validateNote method, to ensure that our Note is not blank or empty before saving. This ensures we don’t save empty notes:

After validating our Note item, we proceed to save it using addNote method.

The above method creates a comment first, as we don’t request user for the comment. We then create an instance of the Note, then pass our note, comment and date values. We then call objectbox to save the record using :

This saves our Note object, and then we log the Note Id. To read our log using Android Studio, we open the Android Monitor tab on the bottom, as shown in the image below

I have clicked save button twice, so I get two log lines, showing the Note ID’s. We have now managed to create and save our first Note. The final code should look like this:

The note has been saved, and we can now display it. We will cover this in our next tutorial.

Kotlin supports or rather has String Interpolation, which allows us to add variables to our string using $, if its a variable or ${} if its a variable inside an object. We will cover this later in a recap article that I will write soon.

Conclusion

We have managed C, Create in our CRUD. In the next tutorial, we will now use RecyclerView to display our Notes once we save them, in very nice CardView displays. I would recommend that you keep practicing with Kotlin, to make you familiar with the syntax.

I would like to hear from you, whether you have suggestions, questions or anything that you feel I need to hear, just feel free and leave a comment below or get me via mail or social media. I will upload the whole source code in Github, and give the link. Feel free to fork, clone or download and play with it. Remember to subscribe. Cheers!

Happy Coding!

 

More From Android Study:

Leave a Reply

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