Advertisement
Guest User

JSON Example

a guest
May 29th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. private static Type type = new TypeToken<ArrayList<Receipt>>() {
  2. }.getType();
  3.  
  4. public static void saveList(Context context) {
  5. SharedPreferences appSharedPrefs = PreferenceManager
  6. .getDefaultSharedPreferences(context);
  7. SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
  8. //Simple output for showing that the program is saving
  9. Log.d("ReceiptList", "Saving main list...");
  10. Gson gson = new Gson();
  11. //MainList is the arrayList that I'm saving
  12. String json = gson.toJson(mainList);
  13. //ReceiptList is the identifier that I'm using to retrieve the value
  14. prefsEditor.putString("ReceiptList", json);
  15. prefsEditor.apply();
  16. }
  17.  
  18.  
  19. public static void loadList(Context context) {
  20. SharedPreferences appSharedPrefs = PreferenceManager
  21. .getDefaultSharedPreferences(context);
  22. SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
  23. //Simple output for showing that the program is loading
  24. Log.d("ReceiptList", "Loading main list...");
  25. Gson gson = new Gson();
  26. String json = appSharedPrefs.getString("ReceiptList", "");
  27. mainList = gson.fromJson(json, type);
  28. if (mainList == null) {
  29. //Haven't saved before, create a new list
  30. mainList = new ArrayList<Receipt>();
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement