您好,我有一个用户在我的应用程序中填写的表单,其中包括以下内容: 标题、用户名、密码、网址和注释。 我当前的代码存储一个条目的数据没有问题,但是当我输入一个新条目时,数据会被覆盖。我需要能够存储表单的多个条目。我需要对代码进行哪些更改才能实现此功能?
字符串是通过我的程序中的其他代码加密的,这就是为什么它被 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);
}
最佳答案
如果您想为每个用户存储多个表单,则不能为每个表单使用相同的文档 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
字段上的过滤器查询“用户”即可。
关于java - 如何存储具有相同名称但不同条目的多组数据 firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60656612/