【问题标题】:access the .push key of firebase database for updating Profile info in android访问firebase数据库的.push键以更新android中的配置文件信息
【发布时间】:2017-11-06 18:00:54
【问题描述】:

我正在使用 Firebase 开发一个 android 应用程序,因此使用 .push 方法将数据保存到 Firebase 并将它们显示到 list 中,我想更新存储在数据库中的 UserInfo,但我对如何访问感到困惑push 方法生成 Key 以更新配置文件信息,或以任何其他方式在经过身份验证的用户 ID 下设置配置文件信息并读取所有这些数据,以便它将在我显示的列表中更新。

或通过User_id 在子值中访问它的任何其他方法?

myOnBtn点击下面的代码

btn_register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (Validation()) {
                final String name = Field_fullName.getText().toString();
                String phone = firebaseUser.getPhoneNumber();
                String bg = field_bloodGroups.getSelectedItem().toString();
                String DOB = Field_dateOfBirth.getText().toString();
                String state = Field_state.getText().toString();
                final String country = Field_country.getText().toString();
                String city = Field_city.getText().toString();
                UserSignUp userSignUp = new UserSignUp(name, phone, bg, DOB, state, country, city, firebaseUser.getUid());
                DBref.child("DONORS").child(country).push().setValue(userSignUp).addOnCompleteListener(reg.this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(reg.this, DBref.getKey() + "", Toast.LENGTH_SHORT).show();
                            Toast.makeText(reg.this, "User Created", Toast.LENGTH_SHORT).show();
                            editor = sharedPreference.edit();
                            editor.putInt("UserReg", 1);
                            editor.putString("UserCountry", country);
                            editor.putInt("FirstTimeRun", 1);
                            editor.apply();
                            startActivity(new Intent(reg.this, Main2Activity.class));
                            finish();
                        }
                    }
                });
            }
        }
    });

【问题讨论】:

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


    【解决方案1】:

    当您使用push() 方法生成唯一密钥时,您可以像这样获取和存储密钥:

    String key = yourRef.getKey();
    

    要更新记录,请使用以下代码行:

    FirebaseDatabase.getInstance().getReference()
        .child("Pakistan")
        .child(key)
        .child("user_ID")
        .setValue("123456789");
    

    编辑:

    DatabaseReference keyRef = DBref.child("DONORS").child(country).push();
    String key = keyRef.getKey();
    Log("TAG", key);
    keyRef.setValue(userSignUp).addOnCompleteListener(/* ... */);
    

    一旦你有了这个key,你就可以在其他DatabaseReference中使用它。当您在onComplete() 中使用DBref.getKey() 时,方法没有得到推送的密钥,只是得到了DBref 的密钥。

    【讨论】:

    • 不,我试过了,通过添加到我正在写入的数据中,并且通过烘烤这两个领带,它与生成的密钥不同。时间戳:(
    • 这意味着您使用了两次push() 方法。只需要一次。分享一些代码。
    • 我添加了请检查:)
    • 我检查 toast 以获取 Key ,但它为空
    • 能否进一步解释一下,keyRef refrence 必须用于写入数据或何时需要更新数据?
    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2014-12-08
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多