Java 如何将来自不同活动的数据保存到同一个表中?

Java 如何将来自不同活动的数据保存到同一个表中?,java,android,firebase,firebase-realtime-database,firebase-authentication,Java,Android,Firebase,Firebase Realtime Database,Firebase Authentication,下面是我这两项活动的代码。通过这样的编码,数据可以这样存储在firebase中。这是存储数据的快照: 这就是我得到的: 我想要的是这个 我知道会发生这种情况,因为我有两个数据库类,但我试图放入一个类,但没有成功。我想这是因为我的密码 以下是我的代码: Registration ACtivity`public class RegisterActivityUser extends AppCompatActivity { ImageView ImgUserPhoto; stati

下面是我这两项活动的代码。通过这样的编码,数据可以这样存储在firebase中。这是存储数据的快照:

这就是我得到的:

我想要的是这个

我知道会发生这种情况,因为我有两个数据库类,但我试图放入一个类,但没有成功。我想这是因为我的密码

以下是我的代码:

Registration ACtivity`public class RegisterActivityUser extends AppCompatActivity {

    ImageView ImgUserPhoto;
    static int PReqCode=1;
    static int REQUESTCODE=1;
    Uri pickedImgUri;


    //*************** Firebase User Auth **********//
    private EditText userEmail, userPassword, userPassword2, userName, userWeight, userHeight;
    private ProgressBar loadingProgress;
    private Button regBtn;
    private FirebaseAuth mAuth;


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


        //*************** Login Account User *************//

        //Ini Views
        userEmail=findViewById(R.id.redMail);
        userPassword=findViewById(R.id.regPassword);
        userPassword2=findViewById(R.id.regPassword2);
        userName=findViewById(R.id.regName);
        loadingProgress=findViewById(R.id.progressBar);
        regBtn = findViewById(R.id.regBtn);
        loadingProgress.setVisibility(View.INVISIBLE);
        userWeight=findViewById(R.id.userWeight);
        userHeight=findViewById(R.id.userHeight);


        mAuth= FirebaseAuth.getInstance();

        regBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                regBtn.setVisibility(View.INVISIBLE);
                loadingProgress.setVisibility(View.VISIBLE);
                final String email=userEmail.getText().toString().trim();
                final String password=userPassword.getText().toString();
                final String password2=userPassword2.getText().toString();
                final String name=userName.getText().toString().trim();


                if (email.isEmpty() || name.isEmpty() || password.isEmpty() || password2.isEmpty() || !password.equals(password2)){

                    //something goes wrong, all fields must be filled
                    //we need to display error message
                    showMessage("Please Fill All Details Above");
                    regBtn.setVisibility(View.VISIBLE);
                    loadingProgress.setVisibility(View.INVISIBLE);
                }
                else {
                    //everything is okay
                    //Create New User Account

                    CreateUserAccount(email,name,password);


                }
            }
        }) ;


        ImgUserPhoto = findViewById(R.id.editUserPhoto) ;
        ImgUserPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT>=23){
                    checkAndRequestForPermission();
                }
                else {
                    openGallery();
                }
            }
        });

    }

    // ************* Create User Account  *************//
 private void CreateUserAccount(final String email, final String name, String password) {
        //this method to create user account with specific email and password;

        mAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            //user account created successfully

                            User user = new User(
                                    name,
                                    email
                            );

                            FirebaseDatabase.getInstance().getReference("Users")
                                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                    .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //progressBar.setVisibility(View.GONE);
                                    if (task.isSuccessful()) {
                                        //acc successfully registered
                                        showMessage("Account Created");

                                        //after we created account, we need to update the user profile picture
                                        updateUserInfo (name ,pickedImgUri,mAuth.getCurrentUser());
                                    }

                                     else{
                                        showMessage("User Already Have Account" + task.getException().getMessage());
                                        regBtn.setVisibility(View.VISIBLE);
                                        loadingProgress.setVisibility(View.INVISIBLE);
                                    }

                                }
                            });


                            }
                        else {

                            // user account failed

                        }
                    }
                });

    }


    // ************* update user name and photo  ************* //
    private void updateUserInfo(final String name, Uri pickedImgUri, final FirebaseUser currentUser) {

        StorageReference mStorage = FirebaseStorage.getInstance().getReference().child("users_photos");
        final StorageReference imageFilePath = mStorage.child(pickedImgUri.getLastPathSegment());
        imageFilePath.putFile(pickedImgUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                //image Uploaded Successfully
                //now we can get our image URL

                imageFilePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        //uri contain user image URL
                        UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                                .setDisplayName(name)
                                .setPhotoUri(uri)
                                .build();

                        currentUser.updateProfile(profileUpdate)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if(task.isSuccessful()){


                                            // User Profile has updated successfully

                                            showMessage("Register Complete");
                                            updateUI();
                                        }


                                    }
                                });


                    }
                });


            }
        });


    }

    private void updateUI() {

        Intent editProfileActivity = new Intent(RegisterActivityUser.this,EditProfileActivity.class);
        startActivity(editProfileActivity);
        finish();


    }

    // ************* simple method to toast message  *************//
    private void showMessage(String message) {

        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
    }

    //  ************* Upload Picture  *************//
    private void openGallery() {
        //TODO: open gallery intent and wait user to pick an image!
        Intent galleryIntent=new Intent(Intent.ACTION_GET_CONTENT);
        galleryIntent.setType("image/*");
        startActivityForResult(galleryIntent,REQUESTCODE);

    }
    private void checkAndRequestForPermission() {
        if (ContextCompat.checkSelfPermission(RegisterActivityUser.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(RegisterActivityUser.this,Manifest.permission.READ_EXTERNAL_STORAGE)){
                Toast.makeText(RegisterActivityUser.this, "Please accept for required Permission",Toast.LENGTH_SHORT).show();
            }
            else
            {
                ActivityCompat.requestPermissions(RegisterActivityUser.this,
                        new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
                        PReqCode);
            }
        }
        else
        {
            openGallery();

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK && requestCode == REQUESTCODE && data !=null){
            //the user has success picked an image
            //we need to save its as reference to a Uri Variable
            pickedImgUri=data.getData();
            ImgUserPhoto.setImageURI(pickedImgUri);

        }
    }
}
`

This one edit profile activity: `private void updateUserInfo(final String weight, final String height, final FirebaseUser currentUser) {

        /*Glide.with(this).load(currentUser.getPhotoUrl()).into(userImage);*/



       mAuth.updateCurrentUser(currentUser).addOnCompleteListener(this, new OnCompleteListener<Void>() {
           @Override
           public void onComplete(@NonNull Task<Void> task) {

               UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                       .build();

               currentUser.updateProfile(profileUpdate)
                       .addOnCompleteListener(new OnCompleteListener<Void>() {
                           @Override
                           public void onComplete(@NonNull Task<Void> task) {
                               if(task.isSuccessful()){

                                   UserProfile user = new UserProfile (
                                           weight,
                                           height
                                   );

                                   FirebaseDatabase.getInstance().getReference("Users Profile")
                                           .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                           .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                       @Override
                                       public void onComplete(@NonNull Task<Void> task) {
                                           //progressBar.setVisibility(View.GONE);
                                           if (task.isSuccessful()) {
                                               //acc successfully registered
                                               showMessage("Account Updated");}

                                           else{

                                           }

                                       }
                                   });



                               }


                           }
                       });
注册活动`公共类RegisterActivityUser扩展AppCompativeActivity{
ImageView ImgUserPhoto;
静态int-PReqCode=1;
静态int REQUESTCODE=1;
Uri pickedImgUri;
//***************Firebase用户身份验证**********//
private EditText userEmail、userPassword、userPassword2、userName、userWeight、userHeight;
私有进度条加载进度;
私人按钮注册;
私人消防队;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u register\u user);
//***************登录帐户用户*************//
//Ini视图
userEmail=findviewbyd(R.id.redMail);
userPassword=findviewbyd(R.id.regPassword);
userPassword2=findviewbyd(R.id.regPassword2);
userName=findviewbyd(R.id.regName);
加载进度=findViewById(R.id.progressBar);
regBtn=findviewbyd(R.id.regBtn);
加载progress.setVisibility(View.INVISIBLE);
userWeight=findviewbyd(R.id.userWeight);
userHeight=findviewbyd(R.id.userHeight);
mAuth=FirebaseAuth.getInstance();
regBtn.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图){
regBtn.setVisibility(视图不可见);
加载progress.setVisibility(View.VISIBLE);
最终字符串email=userEmail.getText().toString().trim();
最终字符串密码=userPassword.getText().toString();
最后一个字符串password2=userPassword2.getText().toString();
最终字符串名称=userName.getText().toString().trim();
if(email.isEmpty()| | | name.isEmpty()| | | password.isEmpty()| | password2.isEmpty()| |!password.equals(password2)){
//出了问题,必须填写所有字段
//我们需要显示错误消息
showMessage(“请填写以上所有详细信息”);
regBtn.setVisibility(View.VISIBLE);
加载progress.setVisibility(View.INVISIBLE);
}
否则{
//一切都很好
//创建新用户帐户
CreateUserAccount(电子邮件、姓名、密码);
}
}
}) ;
ImgUserPhoto=findViewById(R.id.editUserPhoto);
ImgUserPhoto.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(Build.VERSION.SDK_INT>=23){
检查并请求权限();
}
否则{
openGallery();
}
}
});
}
//**********创建用户帐户*************//
私有void CreateUserAccount(最终字符串电子邮件、最终字符串名称、字符串密码){
//此方法用于创建具有特定电子邮件和密码的用户帐户;
mAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//已成功创建用户帐户
用户=新用户(
名称
电子邮件
);
FirebaseDatabase.getInstance().getReference(“用户”)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(用户).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
//progressBar.setVisibility(View.GONE);
if(task.issusccessful()){
//acc成功注册
showMessage(“创建的帐户”);
//创建帐户后,我们需要更新用户配置文件图片
updateUserInfo(名称,pickedmguri,mAuth.getCurrentUser());
}
否则{
showMessage(“用户已拥有帐户”+task.getException().getMessage());
regBtn.setVisibility(View.VISIBLE);
加载progress.setVisibility(View.INVISIBLE);
}
}
});
}
否则{
//用户帐户失败
}
}
});
}
//**********更新用户名和照片****************//
私有void updateUserInfo(最终字符串名、Uri pickedImgUri、最终FirebaseUser currentUser){
StorageReference mStorage=FirebaseStorage.getInstance().getReference().child(“用户照片”);
final-StorageReference-imageFilePath=mStorage.child(pickedImgUri.getLastPathSegment());
imageFilePath.putFile(pickedI
 UserProfile user = new UserProfile (
       weight,
       height
 );

 FirebaseDatabase.getInstance().getReference("Users Profile")
       .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
       .setValue(user)
Map<String, Object> values = new HashMap<>();
values.put("height", height);
values.put("weight", weight);
FirebaseDatabase.getInstance().getReference("Users")
       .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
       .updateChildren(values)