尝试从firebase

时间:2017-06-30 17:21:23

标签: java android firebase-realtime-database null data-retrieval

我正在尝试从Firebase读取数据,但它不适用于我。数据库中已有数据,但是当我使用getUserFName时,它表示值为null。所以第二个代码来自register活动,它创建一个身份验证和一个id匹配在一起的数据库。 id保存UserInformation类的数据。所以这就是为什么我想知道为什么我在注册时有数据时会得到一个空对象。

databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    UserInformation userInformation = new UserInformation();
                    userInformation.setUserFName(ds.child(userID).getValue(UserInformation.class).getUserFName());
                    userInformation.setUserLName(ds.child(userID).getValue(UserInformation.class).getUserLName());
                    userInformation.setUserEmail(ds.child(userID).getValue(UserInformation.class).getUserEmail());
                    userInformation.setUserDescription(ds.child(userID).getValue(UserInformation.class).getUserDescription());

                    fName.setText(userInformation.getUserFName());
                    lName.setText(userInformation.getUserLName());
                    email.setText(userInformation.getUserEmail());
                    aboutYou.setText(userInformation.getUserDescription());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
firebaseAuth.createUserWithEmailAndPassword(email.getText().toString().trim(), password.getText().toString().trim())
                                        .addOnCompleteListener(NewUserActivity.this, new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                if (task.isSuccessful()) {
                                                    Toast.makeText(NewUserActivity.this, "Account Created. Sign in!", Toast.LENGTH_SHORT).show();

                                                    String id = firebaseAuth.getCurrentUser().getUid();
                                                    UserInformation userInformation = new UserInformation(firstName.getText().toString(), lastName.getText().toString(), email.getText().toString(),
                                                            password.getText().toString());
                                                    databaseReference.child(id).setValue(userInformation);

                                                    Intent created = new Intent(NewUserActivity.this, LoginActivity.class);
                                                    startActivity(created);
                                                    finish();
                                                } else if (password.getText().toString().length() < 6){
                                                    Toast.makeText(NewUserActivity.this, "Password at least 6 characters", Toast.LENGTH_SHORT).show();
                                                    progressDialog.dismiss();
                                                } else{
                                                    Toast.makeText(NewUserActivity.this, "Email already exists", Toast.LENGTH_SHORT).show();
                                                    progressDialog.dismiss();
                                                }

                                            }
                                        });

1 个答案:

答案 0 :(得分:2)

您正在从错误的位置阅读。

databaseReference.child(userID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
        fName.setText(userInformation.getUserFName());
        lName.setText(userInformation.getUserLName());
        email.setText(userInformation.getUserEmail());
        aboutYou.setText(userInformation.getUserDescription());
    }
}

另外,请确保您在https://firebase.google.com/docs/database/security/quickstart

中拥有Firebase数据库的读取权限