READ – CRUD Using Objectbox

READ – CRUD Using Objectbox

Hi guys! In our previous article, we covered how to create and save Notes using a simple demo app. In this article, we will mainly focus on displaying the Notes that we were able to save in our previous article. Open your project if you had followed, or go back and follow our previous tutorial if you are new.

READ – CRUD Using Objectbox, what does this mean in CRUD? We already saved some data in our database, to show them we perform a READ operation. Saving the notes made them persistent, meaning they are saved “permanently”, but the data can be lost through other ways like clearing the app data or uninstalling the app. But since we saved the data, it remains available to the app and we can query it and display it using RecyclerView and CardViews easily.

Modify User Interface

We had created some UI in the previous article. We are now going to alter or rather add some more code to our previous code. Open activity_main.xml and add the following code:

We are adding a RecyclerView to our layout, which will show the notes that we save. We need to create a new layout file also, and name it notes_item.xml inside our layout folder. Add the following code:

The above layout file acts like a blueprint to generate the Note cards. We then need to create an adapter, which will be responsible for generating and displaying the Note cards. Create a new package, and name it adapter and then create a new Kotlin Class and name it NoteAdapter.kt and add the following code:

We have now created our note view and our adapter. Lets now open our MainActivity.kt and add the following code, modify it a little bit. We first add a LayoutManager to our RecyclerView.

We then create a method to populate our Note items:

Our final code should look like this:

We have now managed to display our notes in simple cards using our recyclerview, using very minimal code thanks to the expressive Kotlin Code. See the screenshot below of our how app should look like:

We have now managed to list our Notes. In the next tutorials, we will now handle deletes and updates.

Conclusion

I would love to hear from you, feel free to leave comments below and I will be glad to help you out. Remember to check our next two tutorials which will wind up this small series, then we can start refreshing Kotlin and working with it.

Happy Coding!

Comments are closed.