0

I'm using a RecyclerView as a checklist that can be used as a grocery list for example, first the user adds the product(item), the price and the store, then my app adds this item object into my ArrayList adapter; each item in my adapter is a relative layout with 3 TextViews for the product, price and store. The problem occurs when the user shuts down his device or closes the app: the RecyclerView resets to nothing.

What is the easiest way to save items in a RecyclerView so that it stays? Do I save in the cache?

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
James A
  • 135
  • 1
  • 4
  • 17
  • Save the data to persistent storage. For something small, put it into a JSON Object, store that JSON as a string in `SharedPreferences`. Then when you re-open the app, populate the `ArrayList` from the storage. http://developer.android.com/guide/topics/data/data-storage.html – RogueBaneling Aug 07 '15 at 01:27
  • So I save my ArrayList data in SharedPreferences? – James A Aug 07 '15 at 01:32
  • Yes, you will need to convert into a `String` first because SharedPreferences can only store primitive types. Therefore the best way to convert to a String is to keep the data structured using JSON. – RogueBaneling Aug 07 '15 at 01:35
  • Sorry but I don't understand the json part – James A Aug 07 '15 at 01:40
  • JSON is like a dictionary, it's a way to store Key-Value pairs. See http://stackoverflow.com/q/383692/3358186 for more information and some examples of what it looks like. Then search for how to use JSON and Android (for reference: http://developer.android.com/reference/org/json/JSONObject.html) – RogueBaneling Aug 07 '15 at 01:43
  • @RogueBaneling Why don't you post your suggestion as an answer? (genuine question) – Kevin Johnson Aug 07 '15 at 01:51
  • Thanks dude helped me a noobie like me a lot – James A Aug 07 '15 at 01:51
  • So I can store a String array, no? It's primitive type – James A Aug 07 '15 at 01:53
  • @KevinJohnson I didn't feel that my original comment was descriptive enough (and instead was more of a keyword list to Google). @JamesA I have updated my answer below, it is possible to store a `Set`. – RogueBaneling Aug 07 '15 at 02:05

2 Answers2

1

Shared Preferences or SQLite Databases

adamin90
  • 216
  • 1
  • 7
1

You will need to save the data to persistent storage in some way. For something small, it would be best to use SharedPreferences. Then, when you re-open the app, load the data from SharedPreferences into your ArrayList, then populate the RecyclerView.

To use SharedPreferences you will need to convert your data into a String, because SharedPreferences can only handle primitive data types. The best way to convert lots of data to a String is by converting the data into a JSONObject, then that JSONObject into a String (which can be stored in SharedPreferences).

JSON is like a dictionary, it's a way to store Key-Value pairs. See this SO question for more information and some examples of what it looks like. Then search for how to use JSON and Android (or reference the docs).

EDIT: If your RecyclerView is showing a simple amount of data, you may be able to store a Set<String> in your SharedPreferences. See this relevant documentation.

Community
  • 1
  • 1
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33