亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確構建 Java 代碼以將命名鍵下的值保存到 Firebase 實時數據庫

如何正確構建 Java 代碼以將命名鍵下的值保存到 Firebase 實時數據庫

元芳怎么了 2022-05-25 17:00:37
我目前正在編寫應用程序的一部分,其中用戶掃描包含用戶 UID 值的 QR 碼。掃描后,我想在我的 Firebase 實時數據庫中創建一個新密鑰,其中包含與具有該 UID 的用戶相關的信息,稱為 userInformation。以下是我希望將值存儲為的格式示例:目前它在沒有 userInfromations 鍵的情況下存儲,它僅與 UID 一起存儲,如下所示: 以下是相關代碼: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 的值,如下所示:目前我只是不確定如何構建我的代碼來獲得這兩個東西。
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

根據最后的評論,要實現如下所示的架構:


rootRef

  |

  --- userInformations

         |

         --- pushId

              |

              --- "userInformationId: "-LXO5 ... H-o3-P" 

              |

              --- "userInformationName: "[email protected]"

請使用以下代碼行:


DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

DatabaseReference userInformationsRef = rootRef.child("userInformations");

String userInformationId = userInformationsRef.push().getKey();

String userInformationName = "[email protected]";

UserInformation userInformation = new UserInformation(userInformationId, userInformationName);

userInformationsRef.child(userInformationId).setValue(userInformation);


查看完整回答
反對 回復 2022-05-25
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號