如何将数据库添加到我的BMI应用程序以保存用户分数?

时间:2018-05-09 14:22:00

标签: java android sqlite android-sqlite

我正在构建一个BMI应用程序。

我想添加一个数据库,以便用户可以保存他们的BMI分数并在需要时查看他们的历史分数。 从根本上说,我如何添加数据库? 我真的很难做到这一点。

如果我是对的,我应该使用SQLite

任何帮助表示赞赏

这是我的代码:

mainactivity.java:

public class MainActivity extends AppCompatActivity {

private EditText height;
private EditText weight;
private TextView result;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    height = (EditText) findViewById(R.id.height);
    weight = (EditText) findViewById(R.id.weight);
    result = (TextView) findViewById(R.id.result);
}

public void calculateBMI(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);
    }
}

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

    if (Float.compare(bmi, 15f) <= 0) {
        bmiLabel = getString(R.string.v_sev_underweight);
    } else if (Float.compare(bmi, 15f) > 0  &&  Float.compare(bmi, 16f) <= 0) {
        bmiLabel = getString(R.string.sev_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.healthy);
    } 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.obese1);
    } else if (Float.compare(bmi, 35f) > 0  &&  Float.compare(bmi, 40f) <= 0) {
        bmiLabel = getString(R.string.obese2);
    } else {
        bmiLabel = getString(R.string.obese3);
    }

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

public void saveBMI() {

}

public void viewBMI() {

}


}

1 个答案:

答案 0 :(得分:-1)

有几种资源可供选择。 您可以参考内容提供商(更容易,您不需要考虑MVVM arch。问题)或者您可以使用领域(check here