您好,我需要我的代码帮助。我正在访问登录到Firebase的当前用户的数据快照。 我想用数据库中的内容动态设置条形图的值。我将全局变量设置在代码的顶部,并将其设置在匿名内部方法中,在该方法中,我调用登录的当前用户的数据快照。当我尝试在该匿名内部方法外部设置条形图条目时,其值表示为null 。我使用了一个日志,并且在内部匿名方法之外,方变量为空,但已将其填充其中。但是我无法在该方法中设置条形图,请帮助我。您可以在我的代码中看到一个Toast方法,在模拟器上,我可以显示所需的脂肪蛋白质和碳水化合物值。在需要设置新的条形图条目的匿名内部方法之外,这些值将变为null。
`public class Health extends AppCompatActivity {
private TextView bmi, weight, height, fitnessGoal, fitnessLevel, calorieIntake;
private FirebaseAuth mAuth;
private BarChart barChart;
private BarData barData;
private BarDataSet barDataSet;
private ArrayList<BarEntry> barEntries = new ArrayList<>();
private Double formattedCalorieIntake;
private Double proteinAmount;
private Double carbohydrateAmount;
private Double fatAmount;
private Double proteinCalories;
private Double carbohydrateCalories;
private Double fatCalories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
bmi = findViewById(R.id.bmiTextField);
weight = findViewById(R.id.currentWeight);
height = findViewById(R.id.currentHeight);
fitnessGoal = findViewById(R.id.currentFitnessGoal);
fitnessLevel = findViewById(R.id.fitnessLevelTextField);
calorieIntake = findViewById(R.id.calorieIntakeTextField);
barChart = findViewById(R.id.barChart);
mAuth = FirebaseAuth.getInstance();
updateFields();
//getBarChartEntries();
XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXaxisValues()));
barDataSet = new BarDataSet(barEntries, "Data Set");
barData = new BarData(barDataSet);
barChart.setData(barData);
barDataSet.setColors(ColorTemplate.MATERIAL_COLORS);
barDataSet.setValueTextColor(Color.BLACK);
barDataSet.setValueTextSize(16f);
}
public void updateFields() {
mAuth = FirebaseAuth.getInstance();
final String userID = mAuth.getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").child(userID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
if (user != null) {
weight.setText(String.valueOf(user.getWeight()));
height.setText(String.valueOf(user.getHeight()));
fitnessGoal.setText(user.getFitnessGoal());
Double heightInMetres = user.getHeight() / 100.00;
Double bodyMassIndex = user.getWeight() / Math.pow(heightInMetres, 2.0);
DecimalFormat decimalFormat = new DecimalFormat("##.00");
Double formattedBodyMassIndex = Double.parseDouble(decimalFormat.format(bodyMassIndex));
bmi.setText(String.valueOf(formattedBodyMassIndex));
if (formattedBodyMassIndex < 18.5) {
fitnessLevel.setText("Underweight");
} else if (formattedBodyMassIndex >= 18.5 && formattedBodyMassIndex <= 24.9) {
fitnessLevel.setText("Healthy Weight");
} else if (formattedBodyMassIndex >= 25.0 && formattedBodyMassIndex <= 29.9) {
fitnessLevel.setText("Overweight");
} else if (formattedBodyMassIndex > 30.0) {
fitnessLevel.setText("Obese");
}
Double weightInPounds = user.getWeight() * 2.20462;
Double requiredCalories = 0.0;
if (user.getActivityLevel().equals("1")) {
requiredCalories = weightInPounds * 14;
} else if (user.getActivityLevel().equals("2")) {
requiredCalories = weightInPounds * 15;
} else if (user.getActivityLevel().equals("3")) {
requiredCalories = weightInPounds * 16;
}
if (user.getFitnessGoal().equals("Lose Weight")) {
requiredCalories = requiredCalories - 500;
} else if (user.getFitnessGoal().equals("Gain Weight")) {
requiredCalories = requiredCalories + 500;
}
formattedCalorieIntake = Double.parseDouble(decimalFormat.format(requiredCalories));
calorieIntake.setText(String.valueOf(formattedCalorieIntake));
if (user.getBodyType().equalsIgnoreCase("Excess body fat")) {
proteinAmount = (user.getWeight() * 2.20462) * 0.75;
} else if (user.getBodyType().equalsIgnoreCase("Average Shape")) {
proteinAmount = (user.getWeight() * 2.20462) * 1;
} else if (user.getBodyType().equalsIgnoreCase("Good Shape")) {
proteinAmount = (user.getWeight() * 2.20462) * 1.25;
}
if (user.getPreferredMacroNutrient().equalsIgnoreCase("Carbohydrates")) {
fatAmount = (user.getWeight() * 2.20462) * 0.3;
} else if (user.getPreferredMacroNutrient().equalsIgnoreCase("Don't have a preference")) {
fatAmount = (user.getWeight() * 2.20462) * 0.35;
} else if (user.getPreferredMacroNutrient().equalsIgnoreCase("Fats")) {
fatAmount = (user.getWeight() * 2.20462) * 0.4;
}
proteinCalories = proteinAmount * 4;
fatCalories = fatAmount * 9;
carbohydrateCalories = formattedCalorieIntake - (proteinCalories + fatCalories);
carbohydrateAmount = carbohydrateCalories / 4;
//values exist but not outside this method
Toast.makeText(getApplicationContext(), "Test:" + fatAmount.floatValue() + "\n" + proteinAmount.floatValue() + "\n" + carbohydrateAmount.floatValue(), Toast.LENGTH_LONG).show();
}
//error coming up as null
barEntries.add(new BarEntry(1,fatAmount.floatValue()));
barEntries.add(new BarEntry(2, carbohydrateAmount.floatValue()));
barEntries.add(new BarEntry(3, proteinAmount.floatValue()));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Health.this, "Error Occurred: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public ArrayList<String> getXaxisValues() {
ArrayList<String> xLabels = new ArrayList<>();
xLabels.add("Fats");
xLabels.add("Fats");
xLabels.add("Carbohydrates");
xLabels.add("Proteins");
ArrayList<String> label = new ArrayList<>();
for (int i = 0; i < xLabels.size(); i++)
label.add(xLabels.get(i));
return label;
}`