This video shows how to implement a settings handler with SQLite for Android applications. It contains also some tricks to make apps like a pro.
Script:
First, we create a new application.
We remove the testing implementation from the project.
We change also the version settings accordingly to the standards.
Now we create an empty activity as a base container for all functions and settings, we want to use in most activities.
This activity needs no layout, thus we remove it from the project.
Since it has no other scope yet, we also may remove the starter code.
All activities, which should use the shared code and settings, extend the base activity.
All functions which are not private to the base activity, become visible to the extending one. This allows us to create fully supported functions once, and use them everywhere, as if they were declared locally.
Now we create a new class, private to the package.
This class will handle the application settings, thus we call it accordingly.
The class extends the inbuilt SQ Lite Open Helper.
In order to implement the missing functions, we press Control O.
We repeat the previous step, for implementing the constructor.
From the constructor, we remove the parameters we do not need.
We define some parameters locally. We could also use constants in place, but as the project grows, it may be more convenient to find everything right on the top.
The database parameter allows us to keep the connection open, as long as the class is needed. This is a speed optimisation, which may have a significant impact on applications with a lot of settings and temporary storage.
This database is opened once, at the initialisation of the class.
At the very first initialisation, this event is fired.
Every time the database version changes from a lower version to a higher, this event is fired. It allows us to update the database easily with new settings.
Here, we typically create a table and add predefined values.
In this table we allow a variable name of up to 32 and a parameter of 128 letters wide. Later I will show you how to store more data.
The first function for use in the applications, is the deletion of all stored variables therein contained.
In the base activity we will now create a reference to this setting class, then available to all extenders.
Since we need this variable ready to use when called by the activities, we need to initialise it from within the base activity.
To access an integer value, we need to implement the respective function in the settings class.
The getter functions require a key for the variable name and a default value.
Now we need a setter function, which requires a key for the variable name and a value.
Since the native integer has no string converter function attached, we wrap it into an integer object.
We start the app and watch the results.
Setting the log tag to our value cleans up the list of events.
Let us create some more functions.
Now we need a string getter and setter.
If we want to use our database also as a temporary data storage, we need to change the value field type.
After this change, we need to recreate our database. I’ll show you how to do this operation.
If we do it in the main activity, the method fails, because the database is held open by the base activity.
The database is now recreated.
We store and retrieve now a string.
-
That’s all for today. Thank you for watching!
En esta página del sitio puede ver el video en línea Android - Handle Settings with a SQLite Database de Duración hora minuto segunda en buena calidad , que subió el usuario Exploding Computers 07 julio 2018, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 58 veces y le gustó 1 a los espectadores. Disfruta viendo!