I have an ArrayList
with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences
but I can't figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences
isn't the way to go about this? Is there a simpler method?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
I have read all answers above. That is all correct but i found a more easy solution as below:
Saving String List in shared-preference>>
}
and for getting String List from Shared-preference>>
Here
Constants.APP_PREFS
is the name of the file to open; can not contain path separators.You can convert it to
JSON String
and store the string in theSharedPreferences
.You could refer the serializeKey() and deserializeKey() functions from FacebookSDK's SharedPreferencesTokenCache class. It converts the supportedType into the JSON object and store the JSON string into SharedPreferences. You could download SDK from here
The best way i have been able to find is a make a 2D Array of keys and put the custom items of the array in the 2-D array of keys and then retrieve it through the 2D arra on startup. I did not like the idea of using string set because most of the android users are still on Gingerbread and using string set requires honeycomb.
Sample Code: here ditor is the shared pref editor and rowitem is my custom object.
You can use serialization or Gson library to convert list to string and vice versa and then save string in preferences.
Using google's Gson library:
Using Java serialization: