shared preferences in android - android

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.

Related

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.

How to save a couple of strings to internal storage in android?

I'm trying to develop a simple notepad on android. But I don't know how to save my notes(strings) to internal storage(or to an SQL database if it's faster). if I used internal storage would I be able to save a couple of strings and get them back? I'm a beginner to mobile application development and this is my first project. so I'd really appreciate it if you could show me a sample code so I can learn from it. Thanks!
A database is an option, therefore you'll definitively have to read the follow page, that helped me a lot. There is also some sample code in it.
http://www.vogella.com/articles/AndroidSQLite/article.html
In paragraph 9.7 is the full code for adding, editing and deleting records...
An other option is saving the string in an .txt file and save that on the storage. Than this might bring you further:
Write a file in external storage in Android
Good luck!
You can save it in shared preference if it is not too big.
To store:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putString("String1", value); // value is the string you want to save
editor.commit()
To retrieve:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
String retrievedString = sharedPref.getString("String1", defaultValue);
"SomeName" ---- the preference name
"String1" ---- key for the string you want to store/retrieve
defaultValue ---- in case the key is not available, this is the retrieved string

Saving large amount of android settings

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);

saving the values in database of sd card in android

I am developing a small android application in which I have to save the values in database.
How should I pursue?
Do you want to use database or shared preferences for your applications? If you are looking for shared preferences, the following is a piece of code that demonstrates how to store and retrieve a username and password.
//Saving the username and password
editor = getSharedPreferences("SampleApps", 0).edit();
editor.putString("userName", "David");
editor.putString("password", "Bravo");
editor.commit();
//Retrieving username and password
SharedPreferences sharedPreferences = getSharedPreferences("SampleApps", 1);
String userName=sharedPreferences.getString("userName", null);
String password=sharedPreferences.getString("password", null);
I hope this is what you are looking for.
In Android, you can use both Shared preferences and database to save the data permanently in the device.
By using Shared preferences, the data are saved in XML format. You can find the data in /data/data/yourPackage/shared_prefs. It is a simple format recommended to have variables.
For more complicated structure, you should use the database that comes with Android, which is SQlite. You can find the database in /data/data/yourPackage/databases SQL sentences could be used to create tables and add values. For easier use, an adapter is recommended.
For more information, there is very good tutorial in the Android official page:
http://developer.android.com/guide/topics/data/data-storage.html

Categories

Resources