如何更改自动生成的值键

How to change auto generated value key

我正在尝试将值放入实时数据库子项中,但值名称看起来像 -MzxnO09HqtrsYUdj2QW。我不知道如何将其更改为姓名、姓氏和电子邮件。谢谢大家

看起来怎么样。

用户

|

我的代码。

user = mAuth.getCurrentUser();
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference().child("No server").child(user.getUid());
                    myRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot datasnapshot) {
                            Name = (String) datasnapshot.child("name").getValue().toString();
                            Lastname = (String) datasnapshot.child("lastName").getValue().toString();
                            Email = (String) datasnapshot.child("email").getValue().toString();

                            txt1.setText(Name);

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnConnectToServer:
               
                    WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    WifiInfo info = wifiManager.getConnectionInfo();
                    String ssid = info.getSSID();
                    serverName = ssid;

                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference().child(ssid);

                    myRef.child(user.getUid()).push().setValue(Name);
                    myRef.child(user.getUid()).push().setValue(Lastname);
                    myRef.child(user.getUid()).push().setValue(Email);

                    connectedToServer = true;

                    break;

        }
    }

使用 push 时,您创建一个 auto-generated 键,see reference:

Create a reference to an auto-generated child location. The child key is generated client-side and incorporates an estimate of the server's time for sorting purposes.

您看到的键(如 -MzxnO08F4QyQTYlsBDG)是那些 auto-generated 子键。

您可能想改用 setValuesetValueAsync。您可以找到 in-depth 有关将数据写入 Firebase 实时数据库的信息 here in the docs