Firebase 实时数据库未更新 // 数据未添加到 Firebase 实时数据库

时间:2021-07-28 15:34:12

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

所以我正在制作一个聊天应用程序,我面临的问题是我试图将用户详细信息添加到我的实时数据库中,但它不起作用,所以我稍后可以将它们显示给登录用户

这是我的注册活动

public class RegisterActivity extends AppCompatActivity {

    EditText username;
    EditText email;
    EditText password;
    Button registerButton;
    TextView oldMember;

    FirebaseAuth auth;
    DatabaseReference myRef;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        username = findViewById(R.id.usernameEditText);
        email = findViewById(R.id.emailEditText);
        password = findViewById(R.id.passwordEditText);
        registerButton = findViewById(R.id.RegisterButton);
        oldMember = findViewById(R.id.LoginTextView);

        
        auth = FirebaseAuth.getInstance();
        myRef = FirebaseDatabase.getInstance().getReference("MyUsers");



        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usernameText = username.getText().toString();
                String emailText = email.getText().toString();
                String passwordText = password.getText().toString();

                if(TextUtils.isEmpty(usernameText) || TextUtils.isEmpty(emailText) || TextUtils.isEmpty(passwordText)){
                    Toast.makeText(RegisterActivity.this, "Please Fill the Required Info", Toast.LENGTH_SHORT).show();
                } else {
                    Register(usernameText, emailText, passwordText);
                }



            }
        });

        
        oldMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }



    private void Register(String username, String email, String password){
        auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull @org.jetbrains.annotations.NotNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            FirebaseUser firebaseUser = auth.getCurrentUser();
                            String userid = firebaseUser.getUid();

                            myRef = FirebaseDatabase.getInstance()
                                    .getReference("MyUsers")
                                    .child(userid);


                            HashMap<String, String> hashMap = new HashMap<>();
                            hashMap.put("id", userid);
                            hashMap.put("username", username);
                            hashMap.put("imageURL", "default");
                            

                            myRef.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull @NotNull Task<Void> task) {
                                    if(task.isSuccessful()){
                                        Intent intent = new Intent(getApplicationContext(), MainHomeActivity.class);
                                        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });

                        } else {
                            Toast.makeText(RegisterActivity.this, "Invalid Email or Password", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

this is how my realtime database looks like

这些是我的实时数据库规则

  "rules": {
    ".read": true,
    ".write": true
  }
}

我将不胜感激!

1 个答案:

答案 0 :(得分:0)

代替使用 HashMap 向 firebase 实时数据库添加数据

 myRef = FirebaseDatabase.getInstance()
                                .getReference("MyUsers")
                                .child(userid);


                        HashMap<String, String> hashMap = new HashMap<>();
                        hashMap.put("id", userid);
                        hashMap.put("username", username);
                        hashMap.put("imageURL", "default");

为信息创建模型类

给定信息的示例模型类

public class userRegisteration(){
 Sting id;
 String username;
 String imageURL;

      //to add details it needs an empty constructor
      userRegisteration(){}

      userRegisteration(String id,String username,String imageURL){
        this.id = id;
        this.username = username;
        this.imageURL = imageURL;
      }
      public void setID(String id){this.id = id}
      public void setUsername(String username){this.username= username}
      public void setImgeURL(String imageURL){this.imageURL = imageURL}

      public String getID(){return id}
      public String getUsername(){return username}
      public String getImageURL(){return imageURL}

}

相信我它会奏效