简体   繁体   English

从 Android Studio 中的实时 firebase 数据库中检索数据

[英]Retrieve data from realtime firebase database in Android Studio

My issue is I can't retrieve data saved in firebase to the user profile.我的问题是我无法将 firebase 中保存的数据检索到用户配置文件。 I would like the user can read the data registered in the database at the user profile.我希望用户可以在用户配置文件中读取数据库中注册的数据。 I have tried several time but the data won't come out.我试了好几次,数据都出不来。 Profile.java:简介.java:

public class profileGuardian extends AppCompatActivity {

  TextInputLayout guardian_username, email, phoneNo, password, address;
  TextView usernameLabel, emailLabel;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_guardian);

    Intent intent = getIntent();
    String username = intent.getStringExtra("username");

    //Hooks
    guardian_username = findViewById(R.id.full_name_profile);
    email = findViewById(R.id.email_profile);
    phoneNo = findViewById(R.id.phoneNo_profile);
    password = findViewById(R.id.password_profile);
    emailLabel = findViewById(R.id.email_field);
    usernameLabel = findViewById(R.id.username_field);

    DatabaseReference rootNode = FirebaseDatabase.getInstance().getReference();
    DatabaseReference reference = rootNode.child("user");

    reference.orderByChild("username").equalTo(username).toString();

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                if (snapshot.child("username").getValue().equals(guardian_username)) {
                    usernameLabel.setText(snapshot.child("username").getValue(String.class));
                    emailLabel.setText(snapshot.child("email").getValue(String.class));
                    guardian_username.getEditText().setText(snapshot.child("username").getValue(String.class));
                    email.getEditText().setText(snapshot.child("email").getValue(String.class));
                    phoneNo.getEditText().setText(snapshot.child("phoneNo").getValue(String.class));
                    password.getEditText().setText(snapshot.child("password").getValue(String.class));
                }
            }
        }

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

        }
    });


  }
}

数据库结构

This seems off:这似乎关闭:

if (snapshot.child("username").getValue().equals(guardian_username)) {

Given that you initialized guardian_username as guardian_username = findViewById(R.id.full_name_profile) , you are comparing a value from the database with a view, which will never be true.鉴于您将 Guardian_username 初始化为guardian_username = findViewById(R.id.full_name_profile) guardian_username您正在将数据库中的值与视图进行比较,这永远不会是真的。

My guess is that you wanted to compare to the username variable:我的猜测是您想与username变量进行比较:

if (snapshot.child("username").getValue().equals(username)) {

Note that the code is needlessly complicated as you can simply access the correct node by its key:请注意,代码是不必要的复杂,因为您可以简单地通过其键访问正确的节点:

DatabaseReference rootNode = FirebaseDatabase.getInstance().getReference();
DatabaseReference reference = rootNode.child("user");

reference.child(username).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        usernameLabel.setText(snapshot.child("username").getValue(String.class));
        emailLabel.setText(snapshot.child("email").getValue(String.class));
        guardian_username.getEditText().setText(snapshot.child("username").getValue(String.class));
        email.getEditText().setText(snapshot.child("email").getValue(String.class));
        phoneNo.getEditText().setText(snapshot.child("phoneNo").getValue(String.class));
        password.getEditText().setText(snapshot.child("password").getValue(String.class));
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // never ignore errors
    }
});
    Intent intent = getIntent();
    String username = intent.getStringExtra("username");
    
    
    DatabaseReference rootReference = FirebaseDatabase.getInstance().getReference();
    
    Query myUsersQuery = databaseReference.child("user").orderByChild("username");
    myUsersQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (snapshot.child("username").getValue().equals(username)) {
                    usernameLabel.setText(snapshot.child("username").getValue(String.class));
                    emailLabel.setText(snapshot.child("email").getValue(String.class));
                    guardian_username.getEditText().setText(snapshot.child("username").getValue(String.class));
                    email.getEditText().setText(snapshot.child("email").getValue(String.class));
                    phoneNo.getEditText().setText(snapshot.child("phoneNo").getValue(String.class));
                    password.getEditText().setText(snapshot.child("password").getValue(String.class));
        }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException();
    }
    }
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM