Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Type type = new TypeToken<ArrayList<Receipt>>() {
- }.getType();
- public static void saveList(Context context) {
- SharedPreferences appSharedPrefs = PreferenceManager
- .getDefaultSharedPreferences(context);
- SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
- //Simple output for showing that the program is saving
- Log.d("ReceiptList", "Saving main list...");
- Gson gson = new Gson();
- //MainList is the arrayList that I'm saving
- String json = gson.toJson(mainList);
- //ReceiptList is the identifier that I'm using to retrieve the value
- prefsEditor.putString("ReceiptList", json);
- prefsEditor.apply();
- }
- public static void loadList(Context context) {
- SharedPreferences appSharedPrefs = PreferenceManager
- .getDefaultSharedPreferences(context);
- SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
- //Simple output for showing that the program is loading
- Log.d("ReceiptList", "Loading main list...");
- Gson gson = new Gson();
- String json = appSharedPrefs.getString("ReceiptList", "");
- mainList = gson.fromJson(json, type);
- if (mainList == null) {
- //Haven't saved before, create a new list
- mainList = new ArrayList<Receipt>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement