How to make android app load data from CSV to SQLite once? - android

I am loading getting data from a csv file then inserting them into SQLite at the launch of my app but it does this every time I open my app. Is there a way that that the app will only iterate through the CSV only on the first use of the app so that the succeeding launches will be quicker?
Thank you in advance :)

You can use SharedPreferences to determine if you already parsed your csv file.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("someKey", "1");
editor.commit();
And before you run your task you should check if you had manipulated your csv already by checking if the "someKey" exsit.

There are several ways that you can avoid to call for CSV file at each time.
You can save a small data (Saving Key-Value Sets) on the app setting using SharedPrefernce.
You can save a small int value on shared preference that indicates you have already called the CSV file or not.
At the beginning you have to call for the CSV load function with in a if conduction that checks whether the sharedpreference value has 1 or 0
if the shared pererence value has 0 the u will enter to the function and insert to the database. after that with in the if condition you will change the shared preference value to 1..
so the next time when you open the app, the shared preference value will be 1... if condition will not be satisfied.. the function will not be called...
Hope u understand

Related

Get shared preferences specific value last updated date and time

I am using shared preferences to store userId once user login to the app. I need to expire(delete) the stored userId after 30 days once the userId is stored on shared preferences.
Is it possible to get the last modified date and time of specific shared preference value.
It's not possible, SharedPreferences don't have any built in method to do this. You'll have to store last modification date manually, possible in SharedPreferences as well.
I would say you can get the path of the SharedPreferences file and try using lastModified() of File class like,
File file = new File("path of shared-preference file");
file.lastModified();
Actually you could create an Alarm: you can set it to run within 30 days from the "registration" for the value stored in SharedPreferences.
You can then use a Receiver (search for more examples) to catch the alarm and run your code.
An example here

Android Saving data on Button

I have a button in a customView which has several numbers on it individually,it counts backward
now my program doesn't keep value on it when user taps back and closes the program How can I prevent this ?
You can pass value from one activity to other using intents. If you want to persist data,the simplest way to achieve this would be saving it in Shared Preferences.
Developer link has good information on how to implement this -
You can save the value Key, Value pair and then retrieve it when the app is up again.
you can Use SharedPreferences its easy to write and get.
You can create one like this
SharedPreferences prefs = getSharedPreferences("yours", MODE_PRIVATE);
and write to like this
SharedPreferences.Editor prefsEditor=prefs.edit();
prefsEditor.putString("what you want", "value of what you want");
prefsEditor.apply();
and retrive like this
String s=prefs.getString("what you want", "value to be retrived if failur happens in retreiving")
if the value not important after end the program you can save it into public static variable
but if you need the variable after end the program you can use SharedPreferences
follow this tutorial http://developer.android.com/reference/android/content/SharedPreferences.html

Android save EditText text

I have a school planer app and I want to save the classes that are written in an EditText object. The problem is, the EditTexts are empty when I close the app and then open it again, and even when I press back. This may be a noob question, but I'm a beginner and I need help. Please answer quickly...
for saving edittext text you need to store the text into storage.
android support storage like
Database
Shared Preferences
For your problem you need to store your Edittext text into the storage when you change value.
And when you open it again you need to retrieve from the storage.
For you best option will be shared preferences. For more Go with This Example . This will make you understand save and retrieve shared preferences.
Short Example
Store Data
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = pref.edit();
editor.putString("first_edittext", "value");
editor.commit();
Retrieve Data
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref.getString("first_edittext", "");

How do I save and read an integer on android libgdx?

How do I save an integer that can be overwritten and then read it on android? I am using libgdx. I know how to do it for my desktop, but I want internal storage.
You should use libgdx Preferences.
That way it will work cross-plattform! (including Android)
Since you just want to store some Integer that should be just what you need.
See example:
//get a preferences instance
Preferences prefs = Gdx.app.getPreferences("My Preferences");
//put some Integer
prefs.putInteger("score", 99);
//persist preferences
prefs.flush();
//get Integer from preferences, 0 is the default value.
prefs.getInteger("score", 0);
There are different ways to do that:
You can write to a FileHandle. Just create a FileHandle like Gdx.files.local("myfile.txt"); and write to it using fileHandle.writeString(Integer.toString(myInt));
Note, that Internal and Classpath are read-only, so you can't write files there.
Also note, that not all types of the gdx.files. are usable for all backends. More on that here.
The second way to do that are the Preferences. The Preferences are the only way to have persistent data for HTML5 applications.
The Preferences are XML files in which you can store, read and change data.
To create Preferences for your app you just need to call:
Preferences myPref = Gdx.app.getPreferences("PreferenceName");
Note, that you should use the full name, for example com.stackoverflow.preferences, as on desktop all Preferences use the same file.
To write data you can use myPref.putInteger("name", value) for Integers, myPref.putBoolean(("name", value) for Booleans... Make sure you call myPref.flush() after all changes, to save the new data.
To get the data you can call myPref.getInteger("name, defaultValue"). The default value is returned if there is no data with the given name.
More on Preferences here.
Hope i could help.

save time stamp in time picker

I am developing a android application where I need time stamp should be saved in time picker even if we click back button in android.Since I am new to android I need help.Thanks in advance for help.
System.currentTimeMillis();
Will get you the current timestamp.If you want to save it somehow you may use SharedPrefences or sqlite db or something like this.
I think I would prefer SharedPreferences since you want the timestamp to be there after clicking the back button. Clicking that button might exit you application so some persistent storage would be fine. The timestampt is no POJO so SharePreferences are my first choice.
Something like that should help:
// That will save the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.edit().putLong("PREF_TAG_TIMESTMAP", System.currentTimeMillis()).commit();
pref.edit().clear().commit();
// That will get you the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.getLong("PREF_TAG_TIMESTMAP", -1.0);
If you need to persist you data for a later run of the activity you can do that using the sql database, shared preferences or if you only need to persist during the current run of the app (not for subsequent launched of the app) you could create a static variable holding the values you want to store for you.
From what I understand you should go with the static variable approach.

Categories

Resources