1

So I'm building a recipe app. I want to show the ingredients in a list in a TextView object for now.

The code below is how I'm doing it at the moment. The ingredients (in ingredientLines) is just put into a string.

JSONObject obj = new JSONObject(response);
JSONArray hits = obj.getJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
    JSONObject a = hits.getJSONObject(i);
    JSONObject recipe = a.getJSONObject("recipe");
    ListItem item = new ListItem(
        recipe.getString("label"),
        recipe.getString("source"),
        recipe.getString("image"),
        recipe.getString("ingredientLines"),
        recipe.getString("url")
    );
}

This is how the string looks now

"ingredientLines" : [ "1 pound multigrain spaghetti (recommended: Barilla Plus)", "Kosher salt", "2 teaspoons black peppercorns", "3 tablespoons unsalted butter", "1 cup coarsely grated Pecorino Romano cheese", "2 full handfuls baby arugula, roughly chopped" ]

I want it to look like

"1 pound multigrain spaghetti (recommended: Barilla Plus)\n Kosher..."

This is the JSON that I am parsing from

"recipe" : {
    "uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_7a38f039e2a9d3df25e65cc64bc0f87d",
    "label" : "The Secret Ingredient (Black Pepper): Multigrain Cacio e Pepe with Arugula Recipe",
    "image" : "https://www.edamam.com/web-img/f59/f59ec1a536535e9072bbf9a7c2432779.jpg",
    "source" : "Serious Eats",
    "url" : "http://www.seriouseats.com/recipes/2011/02/the-secret-ingredient-black-pepper-multigrain-cacio-e-pepe.html",
    "shareAs" : "http://www.edamam.com/recipe/the-secret-ingredient-black-pepper-multigrain-cacio-e-pepe-with-arugula-recipe-7a38f039e2a9d3df25e65cc64bc0f87d/pep",
    "yield" : 4.0,
    "dietLabels" : [ "Balanced" ],
    "healthLabels" : [ "Sugar-Conscious", "Vegetarian", "Peanut-Free", "Tree-Nut-Free", "Alcohol-Free" ],
    "cautions" : [ ],
    "tags" : [ "pasta", "pepper", "The Secret Ingredient", "vegetarian" ],
    "ingredientLines" : [ "1 pound multigrain spaghetti (recommended: Barilla Plus)", "Kosher salt", "2 teaspoons black peppercorns", "3 tablespoons unsalted butter", "1 cup coarsely grated Pecorino Romano cheese", "2 full handfuls baby arugula, roughly chopped" ]
}
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
Joe Tobin
  • 482
  • 3
  • 12

2 Answers2

0

Use String.join("\n", ingredientLines) to transform the array in a string with "\n" between the items.

So, instead of recipe.getString("ingredientLines") use:

String.join("\n", recipe.getJSONArray("ingredientLines"))
Rafael Paulino
  • 570
  • 2
  • 9
  • Android studio is looking for a higher minimum API to use that call. Can you think of any alternatives? That API being 26 which means I'd have to limit my app to Android 8.0 and above which I'm not comfortable doing. – Joe Tobin Feb 21 '18 at 20:07
  • Look here: https://stackoverflow.com/questions/33802971/alternative-for-string-join-in-android – Rafael Paulino Feb 21 '18 at 20:09
0
  ingredients = ingredients.replace("[", "");
  ingredients = ingredients.replace("]", "");
  ingredients = ingredients.replace("\"", "");
  ingredients = ingredients.replace(",", "\n");

This works...

Joe Tobin
  • 482
  • 3
  • 12