Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Storing/retrieving strings with shared preferences

As the title says, I want to save and retrieve certain strings. But my code won't pass through the first line neither in retrieve or store. I tried to follow this link: http://developer.android.com/guide/topics/data/data-storage.html

private void savepath(String pathtilsave, int i) {     String tal = null;     // doesn't go past the line below     SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();     tal = String.valueOf(i);     editor.putString(tal, pathtilsave);     editor.commit(); } 

and my retrieve method:

public void getpaths() {     String tal = null;     // doesn't go past the line below     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);     for (int i = 1; i <= lydliste.length - 1; i++) {         tal = String.valueOf(i);         String restoredText = settings.getString(tal, null);         if (restoredText != null) {             lydliste[i] = restoredText;         }     } } 

lydliste is a static string array. PREFS_NAME is

public static final String PREFS_NAME = "MyPrefsFile"; 
like image 537
Rad Avatar asked Aug 22 '12 13:08

Rad


People also ask

How save and retrieve data using SharedPreferences in Android?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

How data is stored in a shared preference?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

What can I store in shared preferences?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

Where is shared preference stored in Android?

SharedPreferences are stored in an xml file in the app data folder, i.e. SharedPreferences added during runtime are not stored in the Eclipse project. The default shared preferences file would actually be: /data/data/<package>/shared_prefs/<package>_preferences. xml .


2 Answers

To save to preferences:

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MYLABEL", "myStringToSave").apply();   

To get a stored preference:

PreferenceManager.getDefaultSharedPreferences(context).getString("MYLABEL", "defaultStringIfNothingFound");  

Where context is your Context.


If you are getting multiple values, it may be more efficient to reuse the same instance.

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  String myStrValue = prefs.getString("MYSTRLABEL", "defaultStringIfNothingFound");  Boolean myBoolValue = prefs.getBoolean("MYBOOLLABEL", false);  int myIntValue = prefs.getInt("MYINTLABEL", 1); 

And if you are saving multiple values:

Editor prefEditor = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEditor.putString("MYSTRLABEL", "myStringToSave"); prefEditor.putBoolean("MYBOOLLABEL", true); prefEditor.putInt("MYINTLABEL", 99); prefEditor.apply();   

Note: Saving with apply() is better than using commit(). The only time you need commit() is if you require the return value, which is very rare.

like image 165
IAmGroot Avatar answered Oct 12 '22 12:10

IAmGroot


private static final String PREFS_NAME = "preferenceName";  public static boolean setPreference(Context context, String key, String value) {     SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);     SharedPreferences.Editor editor = settings.edit();     editor.putString(key, value);     return editor.commit(); }  public static String getPreference(Context context, String key) {     SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);     return settings.getString(key, "defaultValue"); } 
like image 37
Benito Bertoli Avatar answered Oct 12 '22 10:10

Benito Bertoli