How to save result from calculation into firebase database

Viewed 432

I'm develop a BMI calculator application to save the bmi result as well as height, weight and date, time. Unfortunately, it only saved result for height and weight and the page wont calculate the bmi(when click on calculate button) and wont save the bmi result into database(it saved only height and weight and not the result). Please, really need help. Thank you for your time. Interface

databaseBMIs = FirebaseDatabase.getInstance().getReference("BMIs");

    height = (EditText) findViewById(R.id.height);
    weight = (EditText) findViewById(R.id.weight);
    result = (TextView) findViewById(R.id.result);

    saveBMI = (Button) findViewById(R.id.saveBMI);
    calc = (Button) findViewById(R.id.calc);

    saveBMI.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addBMI();
        }
    });
}

public void bmiCalculator(View v) {
    String heightStr = height.getText().toString();
    String weightStr = weight.getText().toString();

    if (heightStr != null && !"".equals(heightStr)
            && weightStr != null && !"".equals(weightStr)) {
        float heightValue = Float.parseFloat(heightStr) / 100;
        float weightValue = Float.parseFloat(weightStr);

        float bmi = weightValue / (heightValue * heightValue);

    }
}

private void displayBMI(float bmi) {
    String bmiLabel = "";

    if (Float.compare(bmi, 15f) <= 0) {
        bmiLabel = getString(R.string.very_severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
        bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
        bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
        bmiLabel = getString(R.string.normal);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
        bmiLabel = getString(R.string.overweight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
        bmiLabel = getString(R.string.obese_class_i);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
        bmiLabel = getString(R.string.obese_class_ii);
    } else {
        bmiLabel = getString(R.string.obese_class_iii);
    }

    bmiLabel = bmi + "\n\n" + bmiLabel;
    result.setText(bmiLabel);

}

public void addBMI() {
    String height_bmi = height.getText().toString();
    String weight_bmi = weight.getText().toString();
    String result_bmi = result.getText().toString();
    calendar = Calendar.getInstance();
    simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    final String datetime = simpleDateFormat.format(calendar.getTime());

    if (!TextUtils.isEmpty(height_bmi)) {

    } else {
        Toast.makeText(this, "Please enter your height", Toast.LENGTH_LONG).show();
    }

    if (!TextUtils.isEmpty(weight_bmi)) {

        String id = databaseBMIs.push().getKey();

        BMI Bmi = new BMI(id, height_bmi, weight_bmi, result_bmi, datetime);

        databaseBMIs.child(id).setValue(Bmi);

        Toast.makeText(this, "BMI added", Toast.LENGTH_LONG).show();

    } else {
        Toast.makeText(this, "Please enter your weight", Toast.LENGTH_LONG).show();
    }
}

XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/gradient_background4"
    android:orientation="vertical"
    tools:context=".Page3Activity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="65dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="3dp"
        app:cardUseCompatPadding="true"
        app:cardPreventCornerOverlap="false">

        <RelativeLayout
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tv1"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:gravity="center"
                android:text="Height"
                android:layout_marginTop="30dp"/>

            <EditText
                android:id="@+id/height"
                android:layout_below="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:ems="6"
                android:inputType="number|numberDecimal"
                android:textSize="20sp"
                android:gravity="top"/>

            <TextView
                android:id="@+id/tv2"
                android:layout_below="@id/height"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:gravity="center"
                android:textStyle="bold"
                android:layout_gravity="center_horizontal"
                android:text="Weight"
                android:layout_marginTop="50dp"/>

            <EditText
                android:layout_below="@+id/tv2"
                android:id="@+id/weight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:ems="6"
                android:inputType="number|numberDecimal"
                android:textSize="20sp"
                android:gravity="top"/>

            <Button
                android:id="@+id/calc"
                android:layout_below="@id/weight"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="30dp"
                android:text="Calculate"
                android:background="#df5d46"
                android:onClick="bmiCalculator"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:id="@+id/saveBMI"
                android:layout_below="@id/weight"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="160dp"
                android:text="Save"
                android:background="#df5d46"
                android:layout_gravity="center_horizontal"/>

            <TextView
                android:id="@+id/result"
                android:layout_below="@+id/calc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:gravity="center"
                android:textStyle="bold"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"/>


        </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>
2 Answers

In the code that you have shared bmiCalculator() is never called and is displayBMI(). These methods are necessary to display the result. The method bmiCalculator() should probably be called in addBMI() and then call displayBMI() with those results.

The code should look something like this:

var calculation = bmiCalculator();
displayBMI(calculation);

You should call the displayBMI method () At the end of the addBMI method

For example

public void bmiCalculator(View v) {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();

if (heightStr != null && !"".equals(heightStr)
        && weightStr != null && !"".equals(weightStr)) {
    float heightValue = Float.parseFloat(heightStr) / 100;
    float weightValue = Float.parseFloat(weightStr);

    float bmi = weightValue / (heightValue * heightValue);

     displayBMI(bmi)//<<<<<Very important

    }
}

Successfully Friend

Related