0

I have this code to populate a TextView from a db :

String ingredients = "";
    ArrayList<Map<String, Object>> recipeIngredients = db
            .getRecipeIngredients(recipeId);
    for (Map<String, Object> ingredient : recipeIngredients)
        ingredients += "\n"
                + ingredient.get(RECIPES_INGREDIENTS.ingredientNeed).toString() + " "
                + ingredient.get(RECIPES_INGREDIENTS.unit) + " "
                + ingredient.get(RECIPES_INGREDIENTS.ingredient).toString();
    ((TextView) findViewById(R.id.recipeIngredients)).setText(ingredients.substring(1));

It works great, but the problem is that, all the ingredients and units are shown in the same textview, i can't get the code to work to show ingredients and units in separate textviews.

Any help is appreciated.

Thanks

daviddev38
  • 33
  • 7
  • It looks like you're adding all the text to the one TextView defined in your xml. Can you post your layout xml? It sounds like you might want to create a new TextView for each rown in the database. Take a look at this post: http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview – Daniel Nugent Feb 24 '15 at 23:26
  • I have only one textview in the layout. even if i put 3 textview it doesnt work. – daviddev38 Feb 24 '15 at 23:37
  • If you want to use multiple TextView fields, you either need to define them in the xml, or create them dynamically in the code (see link above). – Daniel Nugent Feb 25 '15 at 00:04

1 Answers1

0

You should really create the text views dynamically, but just to get you started and answer your question, here is how you could do it if you always have a static amount of TextViews defined.
The key is that you need to reference each TextView defined in the layout xml separately.

You would need to define a TextView for each ingredient, and a TextView for each unit (in this case a total of 6 TextViews, 3 ingredients and 3 units).

ArrayList<Map<String, Object>> recipeIngredients = db
        .getRecipeIngredients(recipeId);
int i = 0;
for (Map<String, Object> ingredient : recipeIngredients){

  String ingredients = ingredient.get(RECIPES_INGREDIENTS.ingredientNeed).toString()  + " "
            + ingredient.get(RECIPES_INGREDIENTS.ingredient).toString();
  String unit = ingredient.get(RECIPES_INGREDIENTS.unit);
  if (i == 0){
     ((TextView) findViewById(R.id.recipeIngredientsFirst)).setText(ingredients);
     ((TextView) findViewById(R.id.recipeUnitFirst)).setText(unit);

  }
  else if (i == 1){
     ((TextView) findViewById(R.id.recipeIngredientsSecond)).setText(ingredients);
     ((TextView) findViewById(R.id.recipeUnitSecond)).setText(unit);
  }
  else if (i == 2){
     ((TextView) findViewById(R.id.recipeIngredientsThird)).setText(ingredients);
     ((TextView) findViewById(R.id.recipeUnitThird)).setText(unit);
  }
  i++;
}

As you can probably see, it's going to get messy quickly. You probably won't have the same number of ingredients in each recipe, so you don't want to have the layout set in stone in the layout xml.

It might be better to use a ListView and ListAdapter. Take a look at this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137