Reputation: 830
I'm having a bit of a conundrum..
I have a variable called "results" which is the result of a previously calculated variable and then appended either +10%, +20%, 0%(the same as it is..) -10% and so on depending on which radio button the user selects, trouble is the data is only calculated when the user clicks the button. So far my button works fine:
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCalcCalories:
double weight = Integer.parseInt(etWeight.getText().toString());
double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
;
lbm = weight * (100 - bodyfat) / 100;
bmr = 370 + (21.6 * lbm);
maintCals = bmr * actLevel;
maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));
lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
+ "Kg");
bmrResult.setText("Your Base Metabolic rate is "
+ String.valueOf(bmr) + " calories");
calorieResult.setText("Your Daily calorie goal is " + String.valueOf(calResult) + " calories");
Although I need to obtain wether the users wishes to lose/gain/maintain weight using a radiogroup like this:
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radLW:
setGPercentage = 10 * maintCals/100;
calResult = maintCals - setGPercentage;
break;
case R.id.radLWF:
setGPercentage = 20 * maintCals/100;
calResult = maintCals - setGPercentage;
break;
case R.id.radM:
setGPercentage = 0;
break;
case R.id.radGW:
setGPercentage = 10 * maintCals/100;
calResult = maintCals + setGPercentage;
break;
case R.id.radGWF:
setGPercentage = 20 * maintCals/100;
calResult = maintCals + setGPercentage;
break;
}
To clarify, I need 'calResult' to be calculated depending on what the user has selected, obviously this might be a problem if the variables are only calculated during the onClick method :/
Upvotes: 0
Views: 1369
Reputation: 830
final radiogroup:
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radLW:
setGPercentage = 10 * maintCals/100;
finalGPercentage = maintCals - setGPercentage;
break;
case R.id.radLWF:
setGPercentage = 20 * maintCals/100;
finalGPercentage = maintCals - setGPercentage;
break;
case R.id.radM:
setGPercentage = 0;
break;
case R.id.radGW:
setGPercentage = 10 * maintCals/100;
finalGPercentage = maintCals + setGPercentage;
break;
case R.id.radGWF:
setGPercentage = 20 * maintCals/100;
finalGPercentage = maintCals + setGPercentage;
break;
}
and onClick:
case R.id.btnCalcCalories:
double weight = Integer.parseInt(etWeight.getText().toString());
double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
;
lbm = weight * (100 - bodyfat) / 100;
bmr = 370 + (21.6 * lbm);
maintCals = bmr * actLevel;
maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));
lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
+ "Kg");
bmrResult.setText("Your Base Metabolic rate is "
+ String.valueOf(bmr) + " calories");
calorieResult.setText("Your Daily calorie goal is " + String.valueOf(finalGPercentage) + " calories");
break;
I just needed another variable to hold the calculation :D
Upvotes: 1
Reputation: 41427
Your question is not entirely clear, but I think what you need to do is set up some kind of initialization for your activity, so your calResult
has a calculated (default) value before the user makes a selection.
Have a look at the documentation for the Activity
class to better understand the Activty
lifecycle, and initialization-related methods such as onCreate
.
Upvotes: 0