0

我正在开发一个安卓应用程序。我的应用程序应该使用CalculateCalories() 方法根据年龄、体重、身高和性别计算卡路里数量,并将所有值存储在数据库中。

该应用程序适用于男性,但对于女性,它总是给我 0.0 !

这是一段代码。

 public class ProfileActivity extends Activity {

DatabaseAdapter usertable = new DatabaseAdapter(this);
User user = new User();
RadioButton rb_male;
RadioButton rb_female;
RadioButton rb_light;
RadioButton rb_moderate;
RadioButton rb_heavy;
EditText name;
EditText age,weight,height;

String gender;
String level;
float bmr, calnum;

String finalGender;

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.createprofile);

    rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
    // String male = rb_male.getText().toString();

    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    // String female = rb_female.getText().toString();

    rb_light = (RadioButton) findViewById(R.id.lightradiobutton);
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton);
    rb_heavy = (RadioButton) findViewById(R.id.heavyradiobutton);

    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton);

    Button gendercheck = (Button) findViewById(R.id.gendercheck);

    gendercheck.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (rb_male.isChecked()) {
                gender = "male";
            }
            if (rb_female.isChecked()) {
                gender = "female";
            }
            Toast.makeText(ProfileActivity.this, (CharSequence) gender,
                    Toast.LENGTH_LONG).show();

        }
    });

    Button checkactivity = (Button) findViewById(R.id.checkactivity);
    checkactivity.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (rb_light.isChecked()) {
                level = "light";
            }
            if (rb_moderate.isChecked()) {
                level = "moderate";
            }
            if (rb_heavy.isChecked()) {
                level = "heavy";
            }

            Toast.makeText(ProfileActivity.this, (CharSequence) level,
                    Toast.LENGTH_LONG).show();
        }
    });
    addUser.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {



             name = (EditText) findViewById(R.id.namefield);
             age = (EditText) findViewById(R.id.agefield);
            int Age=Integer.parseInt(age.getText().toString());


             weight = (EditText) findViewById(R.id.weightfield);
             int Weight=Integer.parseInt(weight.getText().toString());

             height = (EditText) findViewById(R.id.heightfield);
             int Height = Integer.parseInt(height.getText().toString());


            float calnumber =calculateCalories(Age,Weight,Height,level);

            Log.d("test", "adding");

             name = (EditText) findViewById(R.id.namefield);
             age = (EditText) findViewById(R.id.agefield);
             weight = (EditText) findViewById(R.id.weightfield);
             height = (EditText) findViewById(R.id.heightfield);

            usertable.open();
            long id = usertable.createUser(name.getText().toString(), age
                    .getText().toString(), gender, weight.getText()
                    .toString(), height.getText().toString(), level,calnumber);

            String newId = String.valueOf(id);

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

            Toast.makeText(ProfileActivity.this, newId, Toast.LENGTH_LONG)
                    .show();

            // Cursor c = usertable.getUser(17);
            // if (c.moveToFirst())
            // {
            // Toast.makeText(ProfileActivity.this, "name:"+ c.getString(1)+
            // "age"+c.getString(2), Toast.LENGTH_LONG).show();
            // }

            usertable.close();

            Intent Filling = new Intent();
            Filling.setClass(ProfileActivity.this, FillingActivity.class);
            startActivity(Filling);

        }
    });

}




public float calculateCalories(int age, int weight, int height, String level) {


    if (rb_male.isChecked()) {

         bmr = 66.5f + (13.75f * weight) + (5.003f * height)
                - (6.755f * age);

        if (rb_light.isChecked()) {
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            calnum = bmr * 1.725f;
        }
        if (rb_female.isChecked()) {

            bmr = 665 + (9.563f * weight) + (1.850f * height)
                    - (4.676f * age);

            if (rb_light.isChecked()) {
                calnum = bmr * 1.375f;
            }
            if (rb_moderate.isChecked()) {
                calnum = bmr * 1.55f;
            }
            if (rb_heavy.isChecked()) {
                calnum = bmr * 1.725f;
            }

        }
    }
    return calnum;

}}
4

1 回答 1

1

究竟是什么令人惊讶?

这段代码:

   if (rb_female.isChecked()) {

        bmr = 665 + (9.563f * weight) + (1.850f * height)
                - (4.676f * age);

        if (rb_light.isChecked()) {
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            calnum = bmr * 1.725f;
        }

    }

放置在此检查中:

if (rb_male.isChecked()) {

所以它永远不会被执行。重组你的if/else.

于 2012-04-21T17:19:14.623 回答