我的代码有问题,无法显示新活动的数据(来自先前活动的输入)。
this is my data in the firebase
this is my current output, the BMI is null
what I want is this, the BMI value is not null
这是我当前的HomepageActivity代码:
private void viewBMI() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference myRef = database.getReference("Users").child("bmi").child(uid);
// Read from the database
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String finalBmi = (String) dataSnapshot.getValue();
showBMI.setText("BMI: " + String.valueOf(finalBmi));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// Failed to read value
//Log.w(BMI, "Failed to read value.", error.toException());
}
});
这是上一个活动的代码:
private void updateBMI(double bmi, final FirebaseUser currentUser) {
kg = Double.parseDouble(inputKg.getText().toString());
m = Double.parseDouble(inputM.getText().toString());
metricFormula = new MetricFormula(kg, m);
imperialFormula = new ImperialFormula(kg, m);
//bmi = String.valueOf(TWO_DECIMAL_PLACES.format(metricFormula.computeBMI(metricFormula.getInputKg(),metricFormula.getInputM())));
bmi = Double.parseDouble(TWO_DECIMAL_PLACES.format(metricFormula.computeBMI(metricFormula.getInputKg(),metricFormula.getInputM())));
final double finalBmi = bmi;
mAuth.updateCurrentUser(currentUser).addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
.build();
currentUser.updateProfile(profileUpdate)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Map<String, Object> values = new HashMap<>();
values.put("bmi", finalBmi);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.updateChildren(values).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
//acc successfully registered
showMessage("Account Updated");
updateUI();
}
else{
}
}
});
}
}
});
}
});
}
//next page
private void updateUI() {
Intent homepageActivity = new Intent(EditProfileActivity.this,HomepageUser.class);
//homepageActivity.putExtra("bmi",bmi); //new
startActivity(homepageActivity);
finish();
}
答案 0 :(得分:0)
我认为, 您的参考文献不正确
DatabaseReference myRef = database.getReference("Users").child("bmi").child(uid);
“用户”下没有子级“ bmi”
您的图片中有 用户-> JKkaSDGLdkM7HxzBKf1ivJ4G6A03-> bmi ... etc
您需要考虑另一层信息。
答案 1 :(得分:0)
您对儿童的访问顺序不正确。正如我在Firebase子层次结构的图像中看到的那样,您在“ userID”下有“ bmi”。因此,替换
DatabaseReference myRef = database.getReference("Users").child("bmi").child(uid);
与
DatabaseReference myRef = database.getReference("Users").child(uid).child("bmi");
由于层次结构是Users-> id-> bmi(杰森对象的组成部分)