我想了解当我们从自定义Java对象进行设置时,Firestore如何为文档中的每个值命名键。凭直觉,我认为它必须来自Java类中字段的名称,但事实并非如此。

相反,它似乎来自getter方法名称。那是对的吗?

String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();

Note note = new Note(title, description);

noteRef.set(note)

最佳答案

假设您的noteRef指向与此类似的文档参考:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference noteRef = rootRef.collection("notes").document();


并且Note类中的字段声明为:

private String title;
private String description;


使用以下代码行时:Note note = new Note(title, description);

您的数据库结构如下所示:

Firestore-root
    |
    --- notes (collection)
          |
          --- noteId (document) //Random document Id
               |
               --- title: "titleThatComesFromUserInput"
               |
               --- description: "descriptionThatComesFromUserInput"


文档的关键属性将是titledescription(作为Note类中字段的名称),并且相应的值将是用户在editTextTitleeditTextDescription中输入的确切文本。活动中的观看次数。

关于android - Firestore如何命名通过POJO设置的文档中的键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49751481/

10-11 22:22
查看更多