如何正确构建 Java 代码以将命名键下的值保存到 Firebase 实时数据库

我目前正在编写应用程序的一部分,其中用户扫描包含用户 UID 值的 QR 码。扫描后,我想在我的 Firebase 实时数据库中创建一个新密钥,其中包含与具有该 UID 的用户相关的信息,称为 userInformation。以下是我希望将值存储为的格式示例:

http://img4.mukewang.com/628df0450001b72a04260102.jpg

目前它在没有 userInfromations 键的情况下存储,它仅与 UID 一起存储,如下所示: 

http://img2.mukewang.com/628df0860001361204260076.jpg

以下是相关代码:


case FirebaseVisionBarcode.TYPE_TEXT:

            {

                final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

                userInformations = new ArrayList<>();

                DatabaseReference username = ref.child(item.getRawValue()).child("username");

                DatabaseReference dateOfBirth = ref.child(item.getRawValue()).child("dateOfBirth");


                username.addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override

                    public void onDataChange(DataSnapshot dataSnapshot) {

                        String username = dataSnapshot.getValue(String.class);

                        //Toast.makeText(getApplicationContext(),username, Toast.LENGTH_LONG).show();


                        //it will create a unique id and we will use it as the Primary Key for our UserInfo

                        String id1 = ref.push().getKey();


                        //creating an UserInfo Object

                        UserInformation userInformation = new UserInformation(id1, username);


                        //Saving the Artist

                        ref.child(id1).setValue(userInformation);


                        //displaying a success toast

                        Toast.makeText(getApplicationContext(), username + " has been added", Toast.LENGTH_LONG).show();

                    }

我也意识到通过改变:


final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();


到:


DatabaseReference ref = FirebaseDatabase.getInstance().getReference("userInformations");

我能够获得名为 userInformations 的键,但它不再保存 userInformationName 的值,如下所示:

http://img4.mukewang.com/628df0970001f28f04010080.jpg

目前我只是不确定如何构建我的代码来获得这两个东西。



元芳怎么了
浏览 175回答 1
1回答

呼啦一阵风

根据最后的评论,要实现如下所示的架构:rootRef&nbsp; |&nbsp; --- userInformations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--- pushId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --- "userInformationId: "-LXO5 ... H-o3-P"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --- "userInformationName: "spamemail0100@gmail.com"请使用以下代码行:DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference userInformationsRef = rootRef.child("userInformations");String userInformationId = userInformationsRef.push().getKey();String userInformationName = "spamemail0100@gmail.com";UserInformation userInformation = new UserInformation(userInformationId, userInformationName);userInformationsRef.child(userInformationId).setValue(userInformation);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java