I have an app that has a large chunk of settings consisting of 7 string arrays, 3 strings and a date.Is it recommended to store it into a sqlite db or can I use the preference or is there any other alternative?
Recommended storage for settings and preferences in android is Preferences, you can save StringArray into preference by converting it to Set first, then store into preferences, by method:
prefEditor.putStringSet("key", set);
Related
I'm trying to build a single object that handles all my SharedPreferences, since they are mostly used in the whole app, and I don't quite Understand why it takes two keys to get a value.
The call looks like this:
context.getSharedPreferences(FirstKey, Context.MODE_PRIVATE).getString(SecondKey, default)
I get that its basically built up as a two dimensional array.
The FirstKey gives me a collection of key-value pairs I can use my SecondKey on to get my value. And I get that If I have dozens of SharedPreferences this might come in handy to manage them and prevent mixups/unwanted overwriting.
But is this necessary If I only have like 10 preferences I save anyway or is it reasonable to just use one FirstKey for all of my preferences?
But is this necessary If I only have like 10 preferences I save anyway
or is it reasonable to just use one FirstKey for all of my
preferences?
So for this case you can avoid the use of that FirstKey by using getDefaultSharedPreferences() like this:
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putString("myKey", "myValue");
editor.apply();
or read already set preferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String myPref = prefs.getString("myKey", "myDefaultValue");
I guess it is to encapsulate, organize and manage better groups (first key values) of data you want to store (second keys values). So in case you want for example to retrieve all settings preferences you can group them by a Settings file (first key). Or in case you would like to delete all stored values regarding a user preferences (preferred language, preferred currency.. ) then you can organize those data within a "UserPref" file (first key) and then you can iterate within it to either delete all of them when you logout or whatever pogic you see useful for your user experience.
First key
Retrieve and hold the contents of the preferences file
Second key
Retrieve and hold value in this file
You can use only your "FirstKey" for all your preferences and normally dev us it like this only. You can create multiple instances of shared preference by changing the "First key" at the time of getSharedPreference(). Suppose you want two different shared preference for two different modules in your project then change the "FirstKey" parameter, in this case, you have two be careful while storing and fetching data from preference as you have two different shared preference.
As you rightfully stated, the first key represents a group of key-value pairs (which is actually very similar to a single file) and the second key helps you fetch the values in that file/group.
I personally believe this design is great especially in cases where you may want to separate all your values into different categories. If your app is "small", you can save all your values in one single file/group. Otherwise, you can split all your values into separate files/groups.
I hope this helps.. Merry coding!
What would be the best way to store multiple 3 linked values (String, String, Boolean)? Like in a HashMap for example.
I need to:
save them in SharedPreferences
load them of sp (of course)
change at least the last value dynamically
get all items where the last value is true (for example)
You have three options:
Store it on preferences
Store it in a database
Save a file on disk
If you want to proceed with preferences I will suggest you to convert the 3 value format to a Json format and store it in preferences as json.
{"value1":"value", "value2":"value", "value3":"value"}
or
{"data":"some data",
"link":{
"data":"other linked data",
"link":{...}
}
}
This kind of data is also stored perfectly in a noSQL database. But if you do not want to add a database at all to your project, maybe you can have a look to some noSQL libraries like SimpleNoSQL (it indeed uses a database behind the scenes, but abstracts you very well from it) or Realm (it stores on disk).
using Set in sharedpreference is API lv11;
i have a project need to parse to many types of nested items
and need to save it to sharedpreference using only string,
the xml items is very complicated to save as a normal string to
sharedpreference, if i use normal string it need to create
so many sharedpreference names and values,
My question is, is JSON is the alternative because its string is
like a list so that i can read easily the items in per category.
You may want to consider using internal storage over SharedPreferences.
Here is more information on internal storage.
Yes you can use JSON as a string and save it to the SharedPreference that is because JSON is faster , smaller and less verbose structure than XML.
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.
how to strore data in shared preferences in table format
For store the values using shared preference use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
Editor e= prefs.edit();
e.putString("Name", "AAA");
e.commit();
For retrieve the shared prefs value use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
String s= prefs.getString("Name", "");
I don't think you can save table in sharedpreferences; it is saved as like key-value pair.
You can serialize your object and save it in the preferences. Use Serializable or JSON or Protocol Buffers or whatever you're comfortable with.
SharedPreferences store primitive data in a file as key-value pairs.
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types. You can use SharedPreferences to save any primitive data:
booleans, floats, ints, longs, and strings.
http://developer.android.com/guide/topics/data/data-storage.html#pref
I adviese to use SQLITE Database to store data in table format.
http://developer.android.com/guide/topics/data/data-storage.html#db
Shared preferences = "Key" , "Value" sets.
If we wants to store small amount of data like usernames, passwords then we will go for Shared Preferences.
To store small amount of data there is no need of creation of Database, tables, insertion query and retrieval query --- So better to go for Shared Preferences.
Shared Preferences is to stores small amount of data, where as SQLite3 is to store large amount of data.
Shared preferences works like HashMap in Collections.
Shared preferences data will be stored in xml file. This xml file we can find in the following location.
Go to
Open DDMS perspective.
Select emulator / rooted Device from the left side panel.
Select File explorer tab from the right side panel.
Open data folder.
Again open data folder.
Open our package name.
Here you will find the Shared Preference folder.
Inside this Shared Preference folder - our shared preference xml file will be visible.
We can pull this xml file from the emulator by selecting the xml file and click on Left arrow on top right side of the DDMS window.
We can also change values in the xml file, then we can push this changed xml file into emulator by clicking on the Right arrow which is there on top right side of this DDMS window.
Note: When you push anything into emulator "DONT FORGET TO RESTART YOUR EMULATOR". Otherwise the changes will not be effected.
Storing the values into shared preferences
import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getApplicationContext().getSharedPreferences("PROFILE", 0);
editor=preference.edit();
editor.putString("MANUALPROFILENAME", newProfileValue);
editor.commit();
For getting the values from the shared preferences
import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getBaseContext().getSharedPreferences("PROFILE", 0);
String manualsetunset =preference.getString("MANUALPROFILEENAME", "false");// Here false is default value. If the required string does not found in shared preference, the default value will be stored in the string object.