@Override
 public void onServingDialogPositiveClick(DialogFragment dialog) {
   // Clicked "Save" in servings dialog. Add the food to meal.
   FoodServingFragment frag = (FoodServingFragment) dialog;
   if (replacedFood == null) {
     meal.addFoodItem(frag.food);
     displayToast("Added to meal.", this);
     saveAndFinish();
   } else {
     meal.replaceFoodItem(replacedFood, frag.food);
     displayToast("Replaced food item.", this);
     saveAndFinish();
   }
 }
 // This is for the density dialog, if mulitple matches
 @Override
 public void onDensityDialogPositiveClick(DialogFragment dialog, String name, Double value) {
   // User chose a density item for the food item. Save the food item.
   FoodDensityFragment frag = (FoodDensityFragment) dialog;
   frag.food.setDensity(value);
   frag.food.setDensityName(name);
   if (replacedFood == null) {
     meal.addFoodItem(frag.food);
     displayToast("Added to meal.", this);
     saveAndFinish();
   } else {
     meal.replaceFoodItem(replacedFood, frag.food);
     displayToast("Replaced food item.", this);
     saveAndFinish();
   }
 }
  @Override
  public void onDialogPositiveClick(DialogFragment dialog) {
    // User touched the dialog's positive button - "Add to Meal"
    FoodInfoFragment frag = (FoodInfoFragment) dialog;

    if (replacedFood == null) {
      // First prompt for density or servings
      if (frag.food.usesMass()) {
        if (FoodItem.getAllDensities() == null) {
          displayToast("Error: Cannot access densities.", this);
          return;
        }
        findDensityMatches(frag.food);
      } else if (!frag.food.usesVolume()) {
        DialogFragment servingsDialog = FoodServingFragment.newInstance(frag.food, false);
        servingsDialog.show(getFragmentManager(), "FoodServingFragment");
      } else {
        // Only uses volume, nothing else needed
        meal.addFoodItem(frag.food);
        displayToast("Added to meal.", this);
        saveAndFinish();
      }

    } else {
      // Replace previously added food item
      if (frag.food.usesMass()) {
        if (FoodItem.getAllDensities() == null) {
          displayToast("Error: Cannot access densities.", this);
          return;
        }
        findDensityMatches(frag.food);
      } else if (!frag.food.usesVolume()) {
        DialogFragment servingsDialog = FoodServingFragment.newInstance(frag.food, false);
        servingsDialog.show(getFragmentManager(), "FoodServingFragment");
      } else {
        // Only uses volume, nothing else needed
        meal.replaceFoodItem(replacedFood, frag.food);
        displayToast("Replaced food item.", this);
        saveAndFinish();
      }
    }
  }
 public void findDensityMatchesCallback(Map<String, Double> matches, FoodItem food) {
   if (matches.size() == 0) {
     displayToast("Error: No density matches found.", this);
   } else if (matches.size() == 1) {
     // Only one match found, add it automatically
     Map.Entry<String, Double> entry = matches.entrySet().iterator().next();
     food.setDensity(entry.getValue());
     food.setDensityName(entry.getKey());
     if (replacedFood == null) {
       meal.addFoodItem(food);
       displayToast("Added to meal.", this);
       saveAndFinish();
     } else {
       meal.replaceFoodItem(replacedFood, food);
       displayToast("Replaced food item.", this);
       saveAndFinish();
     }
   } else {
     // Multiple matches. Display dialog list to allow user to pick
     displayToast("Multiple densities found.", this);
     DialogFragment dialog = FoodDensityFragment.newInstance(food, matches);
     dialog.show(getFragmentManager(), "FoodDensityFragment");
   }
 }