Firebaseリアルタイムデータベースのデータの更新 - AndroidのJavaの

Yilmazの浅黒いウィーバー:

私はFirebaseでのユーザープロファイルの編集ページを作ってるんです。私はいくつかの異なる方法を試してみました。しかし、私は更新できませんでした。ただ、データベースに新しいユーザーとして追加。

私は警告ダイアログに新しい値を取得しています。私を助けてください。

私の更新方法のコードです:

public void editAlert() {
        LayoutInflater layoutInflater = LayoutInflater.from(ProfilePage.this);

        View design = layoutInflater.inflate(R.layout.edit_profile, null);

        final EditText editTextUserName = design.findViewById(R.id.username_editTextProfileEdit);

        final EditText editTextRealName = design.findViewById(R.id.realName_editTextProfileEdit);

        final EditText editTextSurname = design.findViewById(R.id.username_editTextProfileEdit);

        final EditText editTextEmail = design.findViewById(R.id.email_editTextProfileEdit);

        final EditText editTextPassword = design.findViewById(R.id.password_editTextProfileEdit);


        AlertDialog.Builder alertDialoga = new AlertDialog.Builder(ProfilePage.this);
        alertDialoga.setTitle("Edit Profile");
        alertDialoga.setView(design);
        alertDialoga.setPositiveButton("Finish", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String username = editTextUserName.getText().toString().trim();
                String realName = editTextRealName.getText().toString().trim();
                String surname = editTextSurname.getText().toString().trim();
                String email = editTextEmail.getText().toString().trim();
                String password = editTextPassword.getText().toString().trim();
                String admin = "false";
                String url = "test_url";


                String key = myRef.push().getKey();

                Users user = new Users(key,username,realName,surname,email,password,url,admin);


                HashMap<String,Object> data = new HashMap<>();

                data.put("user_email", email);
                data.put("user_name", realName);
                data.put("user_password", password);
                data.put("user_surname", surname);
                data.put("username", username);

                myRef.child(user.getUser_id()).updateChildren(data);

                Toast.makeText(ProfilePage.this, "Uptaded!", Toast.LENGTH_SHORT).show();
            }
        });

        alertDialoga.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        alertDialoga.show();
    }

ユーザーコードの作成マイ:

  // Sign Up Method
    // Kullanıcı Kayıt etme metodu
    public void signUp(View view) {
        UUID uuid = UUID.randomUUID();
        final String imageName = "ProfileImages/"+uuid+".jpg";
        final ProgressDialog dialog = new ProgressDialog(signupPage.this);
        dialog.setTitle("Creating user record.. ");
        dialog.setMessage("User registration is in progress..");
        dialog.show();
        StorageReference storageReference = mStorageRef.child(imageName);
        storageReference.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // Url

                StorageReference newReference = FirebaseStorage.getInstance().getReference(imageName);
                newReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        dowloadURL = uri.toString();

                        if (dowloadURL != null) {
                            mAuth.createUserWithEmailAndPassword(emailText.getText().toString(), passwordText.getText().toString())
                                    .addOnCompleteListener(signupPage.this, new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            if (task.isSuccessful()) /* Kullanıcı girişi başarılı ise bu çalışacak */ {
                                                Toast.makeText(signupPage.this, "User Created", Toast.LENGTH_SHORT).show();
                                                String userName = user_name.getText().toString();
                                                String userSurname = user_surname.getText().toString();
                                                String username = user_username.getText().toString();
                                                String user_email = emailText.getText().toString();
                                                String key = myRef.push().getKey();
                                                String password = user_password.getText().toString();
                                                String imageURL = dowloadURL;

                                                Users user = new Users(key, userName, username, userSurname, user_email, password,imageURL, admin);
                                                myRef.push().setValue(user);
                                                Intent homePage = new Intent(signupPage.this, ProfilePage.class);
                                                startActivity(homePage);
                                                finish();
                                                dialog.dismiss();

                                            } else /* Kullanıcı girişi başarısız ise bu çalışacak */ {
                                               /*Intent signBack = new Intent(signupPage.this, signupPage.class);
                                                startActivity(signBack);
                                                finish(); */
                                                dialog.dismiss();
                                            }



                                        }
                                    }).addOnFailureListener(signupPage.this, new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(signupPage.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }


                    }
                });


            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(signupPage.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });


    }

ダウンロードURLが途中で、別の画像選択方法から来ています。私のユーザー作成コードは、このようなものです。

Gauravモール:

あなたの問題ではなく、あなたのfirebaseデータベース内の定数と有効なキーを格納する、あなたのプロフィールを変更するたびに新しいノードを作成することです。どうして?さて、あなたは次の操作を行います。

String key = myRef.push().getKey();

どのたびに(プッシュがある理由です)新しいノードを作成し、そのノードのキーを取得します。新しいユーザーを作成する理由はなく、アカウントのプロフィールを更新するのでもあります。それを行うための正しい方法は以下の通りです。

ユーザーを作成する場合はこれでキーを取得します:

String key = FirebaseAuth.getInstance().getCurrentUser().getUid();

あなたは、このキーを使用してユーザオブジェクトを作成した後、次の手順を実行します。

myRef.child(key).setValue(user);

あなたのユーザーを更新したいときは、キーにあなたがそれを作成したのと同じ方法でアクセスすることができます。すべての更新情報やキーを取得した後、その後の操作を行います。

myRef.child(key).setValue(data); //For updating

若しくは

myRef.child(key).updateChildren(data); //For updating

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=230887&siteId=1