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

Displaying Item and Quantity

+7 votes
I'm stuck when it comes to displaying the quantity along with the name of the grocery item on the list. Where in the code is it setting what is displayed on the screen?
asked Mar 20, 2015 in Spring 2015 by Lindsey Nicolaysen (100 points)

2 Answers

+4 votes
Best answer
Well, I think there are various approaches, but one approach would be:

a) Continue to store groceryname -> quantity mappings in the shared preferences object, just like before.

b) Change the ArrayList<String> to store strings that look like "5 bananas", so that that will be what shows up in the ListView.

c) Every time you go to add something new to the ArrayList, you need to add something more like (quantity + " " + groceryName).

d) Every time they click on something, and you need to figure out what they clicked on, then you need to split the String up into the quantity & name parts (by looking for the first space).  Once you do that, you can use the name part to index into the sharedPreference mapping.
answered Mar 22, 2015 by Forrest Stonedahl (100 points)
selected Mar 23, 2015 by Lindsey Nicolaysen
+4 votes
I know how to initially display both the quantity and grocery item.

The code you are looking to manipulate is somewhere in the method with the comment:

// add new search to the save file, then refresh all Buttons

above it (since I have already changed the names of the method and variables).

Doing something with concatenation of the two strings either in another String variable or simply in the add method will display both values upon addition to the list.  Reference my other question about how to keep the quantity displayed after closing and reopening the application.
answered Mar 20, 2015 by Benjamin Knapper (100 points)