There are many ways to save the data in Android devices. One of them is using the shared preferences more like a key/value pair of data.
How to read the preference?
1) Get the sharedpreference for the preference name
ex) SharedPreferences prefs = ctx.getSharedPreferences(prefName,0);
2) Get all the key value pairs from the preference
ex) Map data = prefs.getAll();
3) Iterate through the map and you can retrive the data for your key.
How to Save the Preference?
1) Get the sharedpreference editor for the preference
SharedPreferences.Editor prefs =
context.getSharedPreferences(getPrefname(), 0).edit();
2) add the key and value to the preference
prefs.putString(newkey, value);
3) Commit your changes
prefs.commit();
How to remove preference?
1)Do all the above steps to get the editor and then use the remove method to remove the preference.
prefs.remove(newkey);
How to read the preference?
1) Get the sharedpreference for the preference name
ex) SharedPreferences prefs = ctx.getSharedPreferences(prefName,0);
2) Get all the key value pairs from the preference
ex) Map
3) Iterate through the map and you can retrive the data for your key.
How to Save the Preference?
1) Get the sharedpreference editor for the preference
SharedPreferences.Editor prefs =
context.getSharedPreferences(getPrefname(), 0).edit();
2) add the key and value to the preference
prefs.putString(newkey, value);
3) Commit your changes
prefs.commit();
How to remove preference?
1)Do all the above steps to get the editor and then use the remove method to remove the preference.
prefs.remove(newkey);
No comments:
Post a Comment