使用uid为当前用户显示RealTime数据库中的某些数据

时间:2019-06-18 22:46:19

标签: java android firebase firebase-realtime-database firebase-authentication

我尝试了许多方法,包括重新排列孩子的位置,但仍然没有用。我想在主页上使用uid从实时数据库显示BMI,但文本视图中的值仍为null。任何人都可以帮助我,因为我仍然是Firebase的新手。

其他信息:BMI值是根据上一个活动计算得出的。

this is my structure from firebase

my current output

what i want

这是主页活动:

public class HomepageUser extends AppCompatActivity {

TextView showBMI;
DatabaseReference myRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage_user);
    showBMI = ( TextView) findViewById(R.id.showBMI);

    //Database
    //FirebaseDatabase database = FirebaseDatabase.getInstance();
    myRef = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("bmi");

}
// Read from the database

@Override
protected void onStart() {
    super.onStart();
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String finalBmi = (String)dataSnapshot.child("bmi").getValue();
            showBMI.setText( ("BMI:   " + finalBmi));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

这是上一个活动的代码:

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();

}

我尝试了很多方法,但是还是没用。

2 个答案:

答案 0 :(得分:0)

您要将侦听器附加到:

myRef = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("bmi");

所以/Users/$uid/bmi。然后,您使用以下命令读取值:

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String finalBmi = (String)dataSnapshot.child("bmi").getValue();
        showBMI.setText( ("BMI:   " + finalBmi));
    }

因此,此时您正在阅读/Users/$uid/bmi/bmi,它不存在。

要解决此问题,请从child("bmi")中删除onDataChange

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String finalBmi = (String)dataSnapshot.getValue();
        showBMI.setText( ("BMI:   " + finalBmi));
    }

答案 1 :(得分:0)

之所以发生这种情况,是因为我为孩子/ Users / $ uid / bmi / bmi附加了双重信息,应该是/ Users / $ uid / bmi

,请注意,除非您像这样分配它们,否则设置文本视图不能将字符串加倍

String bmi =(String)Double.toString((Double)dataSnapshot.getValue());                 showBMI.setText((“ BMI:” + bmi));