开发者社区> 问答> 正文

如何存储具有相同名称但条目不同的多组数据

嗨,我有一个用户填写在我的应用程序中的表格,其中包括以下内容:标题,用户名,密码,webAddress和注释。我当前的代码存储一个条目的数据没有问题,但是当我进入一个新条目时,数据将被覆盖。我需要能够存储表单的多个条目。我需要对代码进行哪些更改才能启用此功能?

字符串通过我程序中的其他代码加密,这就是为什么它被try / catch包围的原因。

 public void storePassword() {
        final String title_entry = title.getText().toString().trim();
        final String username_entry = title.getText().toString().trim();
        final String password_entry = title.getText().toString().trim();
        final String webaddress_entry = title.getText().toString().trim();
        final String note_entry = title.getText().toString().trim();



        Map<String, Object> user = new HashMap<>();
        try {
            user.put("title", EncryptDecrypt.encryptString(title_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("username", EncryptDecrypt.encryptString(username_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("password", EncryptDecrypt.encryptString(password_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("webAddress", EncryptDecrypt.encryptString(webaddress_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        try {
            user.put("note", EncryptDecrypt.encryptString(note_entry, Password));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        userID = firebaseAuth.getCurrentUser().getUid();
        fDatasebase.collection("Users").document(userID).update(user);

    }

问题来源:Stack Overflow

展开
收起
montos 2020-03-28 09:41:14 437 0
1 条回答
写回答
取消 提交回答
  • 如果要为每个用户存储多个表单,则不能为每个表单使用相同的文档ID。您应该使用随机文档ID,并将用户ID作为字段存储在文档中。

    Map<String, Object> user = new HashMap<>();
    // populate the map as you were
    
    // put the UID in the map
    String uid = firebaseAuth.getCurrentUser().getUid();
    user.put("uid", uid);
    
    // Add the document to the colleciton with a random ID
    fDatasebase.collection("Users").add(user);
    

    要获取用户的所有表单,您只需在uid字段上使用过滤器查询“用户”即可。

    回答来源:Stack Overflow

    2020-03-28 09:41:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载