I want to use the shared preferences to save a list of integers. it's simple by using the putStringSet() methode.
final Set<String> set = new TreeSet<String>();
for (final Station station : stations) {
set.add(String.valueOf(station.getId()));
}
editor.putStringSet(USER_STATIONS, set);
but there's a big problem. the list isn't sorted anymore after loading the prefereneces with getStringSet()
Is there an other/better way than save the list as json or as a string with comma seperated values?
I think there is no way to save sorting with preferences (at least SharedPreferences reference is clear about that) , instead you can do it with LiteSQL or by creating a custom file: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal .
Related
I have 10 values (int and boolean) which I want to save, so I can load them whenever I need them (I need the different values at the same time which makes it easy). Is it overkill if I make one JSONString/(JSONObject) out of 10 values and save that string in SharedPreferences? Is it better practice to just store every single value like this:
editor.putInt("Volume", VolumeBar.getProgress());
editor.putInt("Difficulty", DifficultyBar.getProgress());
[...]
and to get it from SharedPreferences like this:
Volume = mPrefs.getInt("Volume", maxVolume);
Difficulty = mPrefs.getInt("Difficulty", 0);
I think it's better to create model with ten variables and convert it
to JsonString because you just put one strng value in SharedPref but it's issue that if you want to update one of it's values you have to retrieve the whole object and modify and set it back in sharedPref
I am using SharedPreferences to save and load my ArrayList like this:
(save)
SharedPreferences loadGlobalVariables = getSharedPreferences(APP_NAME, 0);
Categories = new ArrayList<String>(loadGlobalVariables.getStringSet("Categories", new HashSet<String>()));
(Load)
And this
(they both work fine, both saves and loads correctly)
SharedPreferences saveGlobalVariables = getSharedPreferences(APP_NAME, 0);
SharedPreferences.Editor editor = saveGlobalVariables.edit();
editor.putStringSet("Categories", new HashSet<String>(Categories));
editor.commit();
But the retrieved ArrayList has different element order than before. I know this because I put this ArrayList into a Dialog as a list (this list is refreshed every time I open it.), by Category.toArray(temArray), and the list is no longer alphabetical.
Before, this ArrayList alphabetically sorted String elements inside it. When I retrieve it back from the SharedPreferences, it is no longer alphabetically sorted.
Thank you for you help, in advance.
I had overcome this situation with a trick.
In SharedPreferences first store the size of the ArrayList.
In a for loop store the elements of the arraylist into the SharedPreferences File .
Now here is the trick while storing as key-value pair keep key "ArraylistName" + position of element
While retrieving:
First retrieve the size of the arrayList
for (i = 0 ; i < sizeOfList ; i++)
{ fetch element with key value "ArrayListName" + i }
In Java Set doesn't maintain the order of its elements. Unfortunately SharedPreferences.Editor doesn't provide a way to store a list so you should probably serialize the list into string and use putString().
See Store a List or Set in SharedPreferences.
I have a String array that I want to keep to display in a ListView, but every time I exit the activity, the global String array is deleted and the ListView doesn't display. Is there anyway that I can keep a String array in the application's storage for the next use?
It's actually pretty easy for just a string. You can store it in shared preferences. See this link: http://developer.android.com/guide/topics/data/data-storage.html#pref
I have one listView in my app, which i is use to list the differnt strings by dynamically adding the strings to ArrayList and using that in list view. I have declared this as below
public static List<String> myList = new ArrayList<String>();
this helps me in retaining the values of list even after exiting app, but i cant be able to retain it after i force close the app or cell boots off and on. Is there any way to store this list as we do in shared preference for strings and int?
Couple of methods.
1) Store the entire list as a Single String (delimiter separated) and split it when you read it back (depends on what kind of strings, because if you have delimiters inside the string, you're screwed, although you could use some kinda encoding (Base64))
2) Store them as separate variables "Key0" is the first element, "Key1" is the second...etc You get the idea...Remember to flush the Keys each time you save though.
3) Don't use Shared Preference, instead use a private file and write the entire ArrayList Object (It's serializable.) using a ObjectOutputStream
Cheers.
I've been looking for over the past day and a half at several websites about how to store strings/string arrays/etc. and I can't seem to figure it out. I'm not sure if I'm just not quite comprehending how to implement data storage..or what. But here is my problem, simply put, I have two activity pages (we'll name 'A,B' respectively) . All I want to do is get a string from a text view in Activity B, store it in an array, and then have it accessed by clicking a button in Activity A.
I know it is simple, but I hit a block for some reason... I am trying to use SharedPreferences but how would I obtain the string from Activity B, store it in a global array, and let it be accessed by a different activity (Activity A) ?
Just store it into shared preferences (usually in onPause()):
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(GAME_STATE, writer.toString());
editor.commit();
in one activity and load in another (usually in onResume()):
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String jsonState = preferences.getString(GAME_STATE, null);
And nothing prevents you from using public static variables
You can't store an array directly in shared preferences, but you can store a set.
See putStringSet and getStringSet. You can add all the items from your array to a LinkedHashSet (as long as they are unique) if you wish to store them in SharedPreferences. See Konstantin's answer for the general idea on how to use SharedPreferences.
Yes you can obtain and store value in global array but you will loss everything once you close the application. So its better you create a table and store/get values from the table whenever you required.
Here is an example for creating SQLite example.