为firebase数据库名称的子级提供等于USER ID键的子级

时间:2019-07-08 12:05:59

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

互相问候。 我正在我的项目上,并且缺少如何将用户ID(由数据库创建的用户ID)推送到父“个人资料”中,就像图片一样:- enter image description here

但是我正在得到这个:-

enter image description here

这会导致重写新用户档案的工作,我希望孩子(用户ID)位于父档案中,但不给它任何值,就像第一个picutre一样,这是我的上传代码,

     super.onCreate(savedInstanceState);
        this.getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN  );

        setContentView(R.layout.activity_register);
        name = (EditText)findViewById(R.id.editName);
        email = (EditText)findViewById(R.id.editEmail);
        password = (EditText)findViewById(R.id.editPassword);
        mRegisterbtn = (Button)findViewById(R.id.buttonRegister);
        mLoginPageBack = (TextView)findViewById(R.id.buttonLogin);
        // for authentication using FirebaseAuth.
        mAuth = FirebaseAuth.getInstance();
        mRegisterbtn.setOnClickListener(this);
        mLoginPageBack.setOnClickListener(this);
        mDialog = new ProgressDialog(this);
        mdatabase = FirebaseDatabase.getInstance().getReference().child("profile");



    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        Animatoo.animateCard(Register.this); //fire the slide left animation
    }

    @Override
    public void onClick(View v)  {
        if (v==mRegisterbtn){
            UserRegister();
        }else if (v== mLoginPageBack){
            startActivity(new Intent(Register.this,login.class));
        }
    }

    public void UserRegister() {
        named = name.getText().toString();
        emailed = email.getText().toString();
        passworded = password.getText().toString();

        if (TextUtils.isEmpty(named)){
            Toast.makeText(Register.this, "Enter Name", Toast.LENGTH_SHORT).show();
            return;
        }else if (TextUtils.isEmpty(emailed)){
            Toast.makeText(Register.this, "Enter Email", Toast.LENGTH_SHORT).show();
            return;
        }else if (TextUtils.isEmpty(passworded)){
            Toast.makeText(Register.this, "Enter Password", Toast.LENGTH_SHORT).show();
            return;
        }else if (passworded.length()<6){
            Toast.makeText(Register.this,"Passwor must be greater then 6 digit", Toast.LENGTH_SHORT).show();
            return;
        }
        mDialog.setMessage("Creating User please wait...");
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
        mAuth.createUserWithEmailAndPassword(emailed,passworded).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    sendEmailVerification();
                    mDialog.dismiss();
                    createAnewUser(task.getResult().getUser());
                    mAuth.signOut();
                }else{
                    Toast.makeText(Register.this,"error on creating user", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    //Email verification code using FirebaseUser object and using isSucccessful()function.
    public void sendEmailVerification() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user!=null){
            user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()){
                        Toast.makeText(Register.this,"Check your Email for verification", Toast.LENGTH_SHORT).show();
                        FirebaseAuth.getInstance().signOut();
                    }
                }
            });
        }
    }

    public void OnAuth(FirebaseUser firebaseUser) {
        createAnewUser( firebaseUser);
    }

    public void createAnewUser(  FirebaseUser firebaseUser) {
        User profile = new User(firebaseUser.getUid());
        String userid = firebaseUser.getUid();
        mdatabase.child("email").setValue(getUserEmail());
        mdatabase.child( "id" ).setValue( firebaseUser.getUid() );
        mdatabase.child( "likescount" ).setValue( "0" );
        mdatabase.child( "username" ).setValue( "put your name" );
        mdatabase.child( "photoUrl" ).setValue( "C:\\Users\\Ameer Alnajm\\Desktop\\BOC\\app\\src\\main\\res\\drawable\\facebooklike_websitepng.png" );

    }

1 个答案:

答案 0 :(得分:2)

更改此:

mdatabase.child("email").setValue(getUserEmail());
mdatabase.child( "id" ).setValue( firebaseUser.getUid() );
mdatabase.child( "likescount" ).setValue( "0" );
mdatabase.child( "username" ).setValue( "put your name" );
mdatabase.child( "photoUrl" ).setValue( "C:\\Users\\Ameer Alnajm\\Desktop\\BOC\\app\\src\\main\\res\\drawable\\facebooklike_websitepng.png" );

对此:

DatabaseReference userData = mdatabase.child(userid);
userData.child("email").setValue(getUserEmail());
userData.child( "id" ).setValue( firebaseUser.getUid() );
userData.child( "likescount" ).setValue( "0" );
userData.child( "username" ).setValue( "put your name" );
userData.child( "photoUrl" ).setValue( "C:\\Users\\Ameer Alnajm\\Desktop\\BOC\\app\\src\\main\\res\\drawable\\facebooklike_websitepng.png" );

因为您要检索userId,然后对其添加引用,并使用该引用在userId内添加属性。