Esempio n. 1
0
  /** UPDATE GRAPH WITH SELECTED when results are returned */
  private void updateGraphWithSelected(String results) {

    ndb = new NutritionDatabaseHelper(this);

    if (selectedItems != null || quantities != null) {
      selectedItems.clear();
      quantities.clear();
    } else {
      selectedItems = new ArrayList<Item>();
      quantities = new ArrayList<Integer>();
    }

    int position = -1;
    for (int i = 0; i < ccartList.getCount(); i++) {
      String tempItemName = ccartList.getItem(i);
      int pos = tempItemName.indexOf(" ");
      String compare = tempItemName.substring(pos + 1);

      if (compare.equals(results)) {
        position = i;
        break;
      } else {
        continue;
      }
    }

    Item selectedItem = ndb.getItem(results);
    ndb.close();

    selectedItems.add(selectedItem);
    String itemOne;

    if (selectedItems.get(0).getItemName().length() > 20) {
      itemOne = selectedItems.get(0).getItemName().substring(0, 20) + "...";
    } else {
      itemOne = selectedItems.get(0).getItemName();
    }

    String one = "<font color='#7EAD1A'>" + itemOne + "</font> SELECTED";
    added.setText(Html.fromHtml(one));

    for (GroceryItem gItem : ccart) {
      if (gItem.getItemName().equals(results)) {
        quantities.add(gItem.getQuantity());
        Log.d("CartActivity: ", "Name: " + gItem.getItemName() + quantities.get(0));
      }
    }

    Log.d("CartActivity", "Position: " + position);
    sd_list.setItemChecked(position, true);

    graph.passSelectedItems(selectedItems);
    graph.passSelectedQuantities(quantities);

    graph.postInvalidate();

    graphLabels.setDays(days);
    graphLabels.postInvalidate();
  }
Esempio n. 2
0
  /** TOTAL VALUES Add up all of the values of the current_cart items */
  public PreviousHistory getCartTotalsFor(String username) {
    NutritionDatabaseHelper ndb = new NutritionDatabaseHelper(this);
    AccountDatabaseHelper adb = new AccountDatabaseHelper(this);

    List<GroceryItem> allGItems = adb.getAllGroceryItemsOf(username);
    PreviousHistory cartTotals = new PreviousHistory();
    if (allGItems != null) {
      cartTotals.setId(-1);
      cartTotals.setUsername(username);

      for (int i = 0; i < allGItems.size(); i++) {
        // retrieve the grocery item from the array
        GroceryItem tempGrocery = allGItems.get(i);

        // get the Quantity of the item
        int quantity = tempGrocery.getQuantity();

        // locate the item in the nutrition database
        Item tempItem = ndb.getItem(tempGrocery.getItemName());

        // add the totals to the current cartTotal, remembering
        // to multiply by the quantity!
        cartTotals.setCalories(cartTotals.getCalories() + (tempItem.getCalories() * quantity));
        cartTotals.setProtein(cartTotals.getProtein() + (tempItem.getProtein() * quantity));
        cartTotals.setFat(cartTotals.getFat() + (tempItem.getFat() * quantity));
        cartTotals.setCarbohydrate(
            cartTotals.getCarbohydrate() + (tempItem.getCarbohydrate() * quantity));
        cartTotals.setFiber(cartTotals.getFiber() + (tempItem.getFiber() * quantity));
        cartTotals.setSugar(cartTotals.getSugar() + (tempItem.getSugar() * quantity));
        cartTotals.setCalcium(cartTotals.getCalcium() + (tempItem.getCalcium() * quantity));
        cartTotals.setIron(cartTotals.getIron() + (tempItem.getIron() * quantity));
        cartTotals.setMagnesium(cartTotals.getMagnesium() + (tempItem.getMagnesium() * quantity));
        cartTotals.setPotassium(cartTotals.getPotassium() + (tempItem.getPotassium() * quantity));
        cartTotals.setSodium(cartTotals.getSodium() + (tempItem.getSodium() * quantity));
        cartTotals.setZinc(cartTotals.getZinc() + (tempItem.getZinc() * quantity));
        cartTotals.setVitC(cartTotals.getVitC() + (tempItem.getVitC() * quantity));
        cartTotals.setVitB6(cartTotals.getVitB6() + (tempItem.getVitB6() * quantity));
        cartTotals.setVitB12(cartTotals.getVitB12() + (tempItem.getVitB12() * quantity));
        cartTotals.setVitA(cartTotals.getVitA() + (tempItem.getVitA() * quantity));
        cartTotals.setVitE(cartTotals.getVitE() + (tempItem.getVitE() * quantity));
        cartTotals.setVitD(cartTotals.getVitD() + (tempItem.getVitD() * quantity));
        cartTotals.setVitK(cartTotals.getVitK() + (tempItem.getVitK() * quantity));
        cartTotals.setFatSat(cartTotals.getFatSat() + (tempItem.getFatSat() * quantity));
        cartTotals.setFatMono(cartTotals.getFatMono() + (tempItem.getFatMono() * quantity));
        cartTotals.setFatPoly(cartTotals.getFatPoly() + (tempItem.getFatPoly() * quantity));
        cartTotals.setCholesterol(
            cartTotals.getCholesterol() + (tempItem.getCholesterol() * quantity));
        cartTotals.setDays(days);
      }

      // Log.d("Created: ", "Cart Total for : " + cartTotals.getUsername()
      // + "Total Calories: " + cartTotals.getCalories());
      adb.close();
      ndb.close();
      return cartTotals;
    }
    ndb.close();
    adb.close();
    PreviousHistory emptyHistory = new PreviousHistory();
    emptyHistory.setUsername(currentUsername);
    return emptyHistory;
  }