Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

How do I get the quantity to stay after reopening application

+9 votes
After I close and reopen the app, the quantity that was previously displayed disappears and only the grocery item remains.


It seems as if using this code is almost pointless now:
      SharedPreferences.Editor preferencesEditor = savedSearches.edit();
      preferencesEditor.putString(grocery, quantity); // store current search
      preferencesEditor.apply(); // store the updated preferences

This is because we don't want the "tag" to simply be a name for something else now.  We want to combine the quantity and grocery item into one string to be displayed.  The SharedPreferences interface would be utilized if we wanted to have the grocery item be the quantity?
asked Mar 20, 2015 in Spring 2015 by Benjamin Knapper (100 points)
edited Mar 20, 2015 by Benjamin Knapper

2 Answers

+2 votes
Well, I think you need to think about what information you're storing in the sharedPreferences object, and how to translate that information into (and out of) the ArrayList<String> that's storing what you want to display in the ListView.
answered Mar 22, 2015 by Forrest Stonedahl (100 points)
+2 votes
Basically, in the onCreate method, what's only being transferred from the SharedPreferences to the main ArrayList that is getting displayed (the ArrayList that is sent though setListAdapter) are the keys. So, only the 'grocery' is being transferred back when you open up the app.


I haven't figured out a way to get both the keys and values transferred to the main ArrayList, so I had to go with a method Dr. Stonedahl outlined in a previous question.
answered Mar 22, 2015 by Christian Schroeder (100 points)