无法获取要插入实时Firebase Db的数据

时间:2019-07-09 08:20:39

标签: java android firebase firebase-realtime-database

非常简单,这是我要在Firebase中使用的布局

规则也设置为完全开放。

不确定是不是我遗漏的命名约定,还是我正在使用哈希映射来尝试传递数据块,而不是两次调用.setValue()。

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

        mAuth = FirebaseAuth.getInstance();
        firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null){
                    Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };

        mRegister = findViewById(R.id.register);
        mEmail = findViewById(R.id.email);
        mPassword = findViewById(R.id.password);
        mName = findViewById(R.id.name);

        mRadioGroup = findViewById(R.id.radioGroup);

        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int selectedInstrument = mRadioGroup.getCheckedRadioButtonId();

                final RadioButton radioButton = findViewById(selectedInstrument);

                final String email = mEmail.getText().toString();
                final String password = mPassword.getText().toString();
                final String name = mName.getText().toString();
                mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()){
                            Toast.makeText(RegistrationActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();
                        } else {
                            String Uid = mAuth.getCurrentUser().getUid();
                            DatabaseReference currentUserDb = FirebaseDatabase.getInstance().getReference().child("Users").child(Uid);
                            Map userInfo = new HashMap<>();
                            userInfo.put("name", name);
                            userInfo.put("instrument", radioButton.getText().toString());
                            currentUserDb.updateChildren(userInfo);

                        }
                    }
                });
            }
        });
    }

0 个答案:

没有答案