android - 火存储 : Create a Subdocument of ArrayList inside a Document

标签 android google-cloud-firestore

我有一个名为 Routine 的类,它具有属性名称和练习。 RoutineExercises 类型的 ArrayList 中的练习。

当我将例程写入 Firestore 数据库时,它会按预期工作,并添加一个包含名称的文档和一个数组,用于使用 ArrayList 中的所有对象进行练习。

但是我认为将练习存储在与名称相同的文档中可能不是一个好主意,因为我有时不需要这些练习。所以我想在我的例程文档中创建另一个集合“RoutineExercises”,其中包含例程练习的 ArrayList。这就是我的代码的样子:

fm.getColRefUserRoutines().add(routine).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
    @Override
    public void onSuccess(DocumentReference documentReference) {
        documentReference.collection("RoutineExercises").add(routine.getExcersises()).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
            @Override
            public void onSuccess(DocumentReference documentReference) {
                Log.d("Success", "!!");
            }
        });
    }
});

fm.getColRefUserRoutine()返回我的例程集合。

但我是异常(exception):

java.lang.IllegalArgumentException: Invalid data. Data must be a Map or a suitable POJO object

最佳答案

为了使用 add(),您必须传递 Map 或 Object。如果添加对象,您必须有一个空的构造函数及其属性的所有 getter here .

Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property

如果您想插入练习列表,您可以尝试以下操作:

HashMap<String, Exercise> exerciseMap = new HashMap<String, Exercise (); 
for (Exercise exercise : exerciseList) {
   exerciseMap.put(exercse.getExerciseCode(), exercise);

}

基于this回答。

关于android - 火存储 : Create a Subdocument of ArrayList inside a Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49840957/

相关文章:

android - 使用 MediaRecorder 录制 MPEG TS

java - ArrayIndexOutOfBoundsException : length=20; index=20 when select item from spinner

android - 使用 FirestoreRecyclerAdapter 的 Firestore 分页 (Android)

firebase - 服务帐号的 Firestore 服务规则

node.js - 无法修改云函数中已提交的WriteBatch

android - 在 View 之间切换

android - 无法对非静态方法问题进行静态引用

android - 为什么 addToBackStack() 方法不起作用

reactjs - 约会应用程序 - 我需要在 Firebase Firestore 中使用哪种数据库结构来查询除不喜欢我的用户之外的所有用户?

java - 每当 Firebase Firestore Android 发生变化时如何实现通知?