How To Save And Fetch Integer Value In Shared Preference In Android?
Solution 1:
You can use shared preferences as follows
//To saveSharedPreferencessettings= getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editoreditor= settings.edit();
editor.putInt("SNOW_DENSITY",mSnowDensity);
editor.commit();
//To retrieveSharedPreferencessettings= getSharedPreferences("YOUR_PREF_NAME", 0);
intsnowDensity= settings.getInt("SNOW_DENSITY", 0); //0 is the default value
getSharedPreferences() is a method of the Context class. If you are inside a Activity or a Service (which extend Context) you can use it like in this snippet. Else you should get the context using getApplicationContext() and then call getSharedPreferences() method.
For more options you can refer to the documentation at http://developer.android.com/guide/topics/data/data-storage.html#pref
Solution 2:
To save in the SharedPreferences:
privatefinalStringPREFS_NAME="filename";
privatefinalStringKEY_DENSITY="den";
Contextctx= getApplicationContext();
SharedPreferencessharedPreferences= ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= sharedPreferences.edit();
editor.putInt(KEY_DENSITY, mSnowDensity);
editor.commit();
To get the value:
Contextctx= getApplicationContext();
StringstrSavedValue=null;
SharedPreferencessharedPreferences= ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
strSavedValue = sharedPreferences.getInt("den", anyDefaultValue);
Solution 3:
Save the value in prefrence
privatevoidSavePreferences(String key, int value) {
SharedPreferencessharedPreferences= getPreferences(MODE_PRIVATE);
SharedPreferences.Editoreditor= sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
get the value from preference
privatevoidshowPreferences(String key){
SharedPreferencessharedPreferences= getPreferences(MODE_PRIVATE);
intsavedPref= sharedPreferences.getInt(key, 0);
}
You can use the key as the shared preference name
Solution 4:
Initialization We need an editor to edit and save the changes in shared preferences. The following code can be used to get the shared preferences.
SharedPreferencespref= getApplicationContext().getSharedPreferences("MyPref", 0); // 0 :- for private modeEditoreditor= pref.edit();
Storing Data
editor.putBoolean("key_name", true);
editor.putString("key_name", "string value");
editor.putInt("key_name", "int value");
editor.putFloat("key_name", "float value");
editor.putLong("key_name", "long value");
editor.commit();
Retrieving Data
pref.getString("key_name", null); // getting String
pref.getInt("key_name", -1); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clearing or Deleting Data
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
editor.clear();
editor.commit(); // commit changes
Solution 5:
Saving preference is not a troubling task. But if you have a lot of such configurable options you could use a PreferenceActivity
and override onSharedPreferenceChanged
.
More details here http://developer.android.com/guide/topics/ui/settings.html
Post a Comment for "How To Save And Fetch Integer Value In Shared Preference In Android?"