public void accept(View view) { if (checkCorrectFill()) { if (atEdit) { Toast.makeText(this, "Product changed", Toast.LENGTH_SHORT).show(); nutrientsDBAdapter.updateRowById( intent.getIntExtra(GlycemicIndexDBAdapter.KEY_ROWID, 0), newName.getText().toString().trim(), Float.parseFloat(newProteins.getText().toString()), Float.parseFloat(newFats.getText().toString()), Float.parseFloat(newCarbs.getText().toString()), Float.parseFloat(newCalories.getText().toString())); navigateBack(); } else { Toast.makeText(this, "Product added", Toast.LENGTH_SHORT).show(); // insert nutrientsDBAdapter.insertNewRow( newName.getText().toString().trim(), Float.parseFloat(newProteins.getText().toString()), Float.parseFloat(newFats.getText().toString()), Float.parseFloat(newCarbs.getText().toString()), Float.parseFloat(newCalories.getText().toString())); // clear forms newName.getEditableText().clear(); newProteins.getEditableText().clear(); newFats.getEditableText().clear(); newCarbs.getEditableText().clear(); newCalories.getEditableText().clear(); } } }
private boolean checkCorrectFill() { String name = newName.getText().toString().trim(); float proteins = 0; float fats = 0; float carbs = 0; float calories = 0; if (atEdit) { if (name.length() == 0) { toastText.setText(R.string.tables_enter_product_name); warningToast.show(); return false; } } else { if (name.length() > 0) { if (nutrientsDBAdapter.foodAlreadyExists(name)) { toastText.setText(R.string.tables_product_already_exists); warningToast.show(); return false; } } else { toastText.setText(R.string.tables_enter_product_name); warningToast.show(); return false; } } if (newProteins.getText().toString().isEmpty()) { toastText.setText(R.string.tables_nutrients_proteins_is_empty); warningToast.show(); return false; } else proteins = Float.parseFloat(newProteins.getText().toString()); if (newFats.getText().toString().isEmpty()) { toastText.setText(R.string.tables_nutrients_fats_is_empty); warningToast.show(); return false; } else fats = Float.parseFloat(newFats.getText().toString()); if (newCarbs.getText().toString().isEmpty()) { toastText.setText(R.string.tables_nutrients_carbs_is_empty); warningToast.show(); return false; } else carbs = Float.parseFloat(newCarbs.getText().toString()); if (newCalories.getText().toString().isEmpty()) { toastText.setText(R.string.tables_nutrients_calories_is_empty); warningToast.show(); return false; } else calories = Float.parseFloat(newCalories.getText().toString()); if (proteins + fats + carbs > 100) { toastText.setText(R.string.tables_nutrients_values_to_match); warningToast.show(); return false; } return true; }