Where to put code for Sample data in Android? - android

I have build an android application where I call the method insertSampleData()
from the onCreate() method of main activity.
The problem is that onCreate() method is called every-time the app is launched,
this results in filling my database with a lot of sample data.
I want to provide sample data only once when the application is installed. Can anyone please tell me how to do it ? Thanks a lot !
P.S: The method insertSampleData() is used to insert sample data into Sq-lite database.

2 ways of doing this:
Either save if the data was inserted once (e.g. to the SharedPreferences)
Or check if any sample data is already in your database before adding it (but may lead to problems if the user can erase the data)

You could create a check in your Application class, in its onCreate() similar to this one:
SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
boolean firstRun = preferences.getBoolean("firstRun", false);
if(firstRun) {
insertSampleData();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("firstRun", true);
editor.commit();
}

While other answers are correct, there is a built-in way to achieve what you want: onCreate and onUpgrade methods of SqliteOpenHelper.
About onCreate() and onUpgrade()
onCreate(..) is called whenever the app is freshly installed.
onUpgrade is called whenever the app is upgraded and launched and the
database version is not the same.
As written here: https://stackoverflow.com/a/8133640/5349594
I would add that if you are using some orm library, most of them support something similar to this.

In your insertSampleData() method, before inserting the data, check the row count in yout table.
If it's 0, execute the INSERTs.
No else statement is needed: proceed to exit.
Note: The sample data will be re-inserted on next run, if not found.
Because insertSampleData() is called in onCreate().
And it will insert the sample data, if no data is found.
Therefore, the table will never be empty on start.

As Hrundi said in comments, you can use if statement, but probably better choice would be to use custom DatabaseHelper class wihic extends SQLiteOpenHelper.
Inside SQLiteOpenHelper you have onCreate method which is called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.
You can google this and you will find a lot of examples.

Related

Android Static Variables and Shared Preferences

I am writing an android app and I retrieve a balance for the user from a webservice and the user is able to log in and out of my app.
When the app starts I check the shared preferences to see if the user is logged in or out. On correct log in I update the shared pref boolean to true and set it to false when the user logs out.
I need to know the balance in several fragments and I need to remember it when I am navigating thru the fragments in my app. When I return to the "My Account" fragment balance value is lost and I have to call my web service again to check it.
It the best way to use a string shared pref and update it any time the app starts or when there is a change in the balance. Or am I better to use a static variable in my main activity that can be referenced when the user navigates to the My Account fragment.
Is it possible to overuse shared preferences?
A simple and elegant solution is to use a very simple library TinyDB in android, which is nothing but an abstraction over the Shared Preferences.
You can initialise it in your activity's onCreate() method like this:
TinyDB tinydb = new TinyDB(this);
In fragment, just replace this with getActivity.
And then use it like this:
tinydb.putString("userName", "john");
String currentUser = tinydb.getString("userName");
Hope it helps.
For a single value such as balance or username you should definitely use SharedPrefereneces and OnSharedPreferenceChangeListener.
For structured data such as balance operations you should definitely use a database:
SQLite
Realm (noSQL)
StorIO (SQLite wrapper)
or some ORM.
There is 3 way to store you login details .
Using Shared Preference
Store Data in a File
Using Sqlite.
This tutorial will give you the ideas for storing login details.
You should store data in SharedPreferences rather than global variables because when app crashes, the data updated in static global variables at different times is lost and the default data in it remains.

Store data in SQLite at first app execution

I wonder if it is possible to create a SQLite database when the app got run the first time and already save some data in it.
I don't want to save it every time the app gets started. Just after Installation.
Is this possible?
Just override onCreate() method in class which extends from SQLiteOpenHelper and insert there some data.
Called when the database is created for the first time. This is where
the creation of tables and the initial population of the tables should
happen.
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onCreate(android.database.sqlite.SQLiteDatabase) .
For my apps I do the simple trick as in Splash check preference of available Data variable, first time its default is false. Then I read from Database and check is there any items If No then insert data and set preference available data variable to true.
and when you come next time splash it will check again and in your available data variable will get true so you don't need to do anything and continue your flow.
I hope this will help you too :)
SQLiteOpenHelper.onUpgrade should do the trick, simply compare oldVersion with newVersion

StringSets won't save/load to/from SharedPreferences

My app stores an ArrayList of custom Food objects which are posted to a ListView in the main Activity. The ListView portion of the app works perfectly, but I'm having an issue saving and loading these Food objects to and from SharedPreferences.
I'm saving these Food objects to SharedPreferences by saving each of its attributes as a String, putting them in a StringSet, and saving each one via the putStringSet function. When the app resumes, it reads through each StringSet from SharedPreferences and creates a new Food item with the read attributes, then adds each one to the ArrayList. The code seems like it should work, but just doesn't load the data between sessions. Can anyone help me figure out why? Thank you!
Code removed
You seem to never instantiate foodNamesSet variable. Hence the check if(foodNamesSet != null) always return false. Same thing applies to other sets as well. Besides, I think you should do the logic in onPause() rather in onStop().

android sqlite database already closed error

I have an app with a sqlite database.
I created my own database class that holds an instance of SqliteDatabase . The class implements my queries, open, close, etc. (the class is NOT a singleton).
I have an activity, a service and an appwidget in my app.
Where I need the database, I create an object of my class, open , do stuff and close at the end.
for example in the activity I open the db in onStart and close it onStop.
Everything works great except in the appwidget.
If I need to select data in the appwidget onUpdate, then it's ok.
but when I try to do an UPDATE from the appwidget, I get the "database DATABASE_FLE already closeed" error.
What can it be?
I added some logs where I'm closing the db, and non of those lines execute before this error.. the db should be ok.
Any ideas?
Thanks.
What i am thinking about, is case in which android may close/unload your activity (this may happend for example if there is a little memory left on the device), but in this time your appwidget is still active.
Now, because of your activity is closed, hence you db is closed.
Appwidget tryes to update DB but its is already closed.
Check this scenario but anyway you should take this scenario in consideration. :)
The problem was that I compiled an SQLiteStatment object and saved it for future uses, while my database object got replaced..
so SQLiteStatement needs to re-compile again.
I had something like this:
public void func(SQLiteDatabase db) {
if (mStatement == null)
mStatement = db.compileStatement(SQL);
mStatement.execute();
The problem with this is that the db object changed in the function call, and the first db object that compiled statement was already closed.
The solution was to remove the if-null, and to compile the statement for every db object.

Run application/activity at first time

I need to recognize first launch of my application or activity.
At this time I need to get some information from server create local database and save info to it. What is the best way to do this?
Create any preferences for example FirstLaunch and set true \ false to it.
Check whether my database exists or not.
Something else?
PS. All server calls must be into one transaction. Ormlite supports transactions?
Thanks.
For the "create database at first run"-purpose, you should use an SQLiteOpenHelper, which offers you the onCreate()-method that is called when:
[...] the database is created for the first time.
The Database-file itself will be created for you (you don't have to do this manually). In this method, you can then perform actions like populating your database with standard entry's.
If you want to populate the database with informations you get from your server, there might be a problem when there is no Internet-connection available.
In this case, I would check if there is a connection available:
If there is, get your informations.
If not, show a Toast or some other notification to inform the user.
To determine if your Database has be populated with the standard entry's, you can use the database-version which is also provided by the SQLiteDatabase-class:
When you first create your Database-object, you call
SQLiteOpenHelpers constructor and pass it 0 as the Database
version.
If you successfully populated your database, you use
setVersion()-method to alter it to 1.
Later in the onOpen()-method, which is called when the
database is opened, you can check if the database was populated by
using the getVersion()-method.
If it is populated, call the super-method to open it.
If not, try populating it.
Further more, the getReadableDatabase() / getWritableDatabase()-methods should be called off the main-thread anyways because:
Database upgrade may take a long time, you should not call this method
from the application main thread, including from
ContentProvider.onCreate().
So getting the informations from the Internet can take place in the onCreate() and in the onOpen()-method (if it wasn't successful at the first try). You can (for example) use a Service to do this.
If you want to solve this problem with database:
Create database with MyDatabasaVersion table and store your version in a single row, for example db_version default value is 0. First time when the application starts you check the db_version if 0 you need to start the syncronisation, after it is finishing set the db_version to 1.
The easiest way should be sharedpreferences. you can call it everywhere form the application context and you can put boolean values in it.
Here are all Android storages.
you should try first option Create any preferences for example FirstLaunch and set true \ false to it.

Categories

Resources