I always suggest using an SQLite database for storing your apps’ data. Even if your app doesn’t store a huge amount of data, you may find benefits to using a database over an app’s SharedPreferences (SharedPreferences are essentially settings each app can save). For instance, one of the best things about storing your data in a SQLite database is that Android provides “helper” Classes for grabbing and displaying data directly from the database – the Cursor is your new best friend.
What’s a Cursor?
Android employs a class called the Cursor, the Cursor literally provides a cursor to your database tables and resulting rows. The Cursor points to each result row in your database based on the SQL query it is fed, it provides a point of reference for you to grab the data it’s pointing to out of the database. The concept is a bit strange but it’s extremely effective, a Cursor is nothing but a tool to grab the underlying data you really want, it itself is nothing but a “link” to the data so to speak.
The Spinner (a.k.a. the drop-down menu) widget, using Android helper Classes, can take a Cursor as a direct input and display the requested fields in the Spinner’s selection fields. It’s simple, scalable, and effective at helping you split your data from your code and design, it’s a great efficient approach that is much more difficult to do without a Cursor and SQLite database.
Why This Tutorial?
I hope you enjoy this tutorial, I picked this tutorial because it shows you how to create a Class to interact with the SQLite database. It shows you how to create a new database record (that’s pretty cool right?), it show you how to create a Spinner and fill it with information you added to the database (also very cool) and other great concepts to demonstrate Android and SQLite interaction.
I feel learning to store your app’s data is the first step to creating a truly robust and powerful app. Using SQLite allows you to save your data simply while being able to retrieve and display your data conveniently with Android’s helper methods.
Get started below!