Android Room Database:如何处理实体中的 Arraylist?
2022-08-31 08:30:47
我刚刚实现了Room用于离线数据保存。但是在实体类中,我收到以下错误:
Error:(27, 30) error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
该类如下所示:
@Entity(tableName = "firstPageData")
public class MainActivityData {
@PrimaryKey
private String userId;
@ColumnInfo(name = "item1_id")
private String itemOneId;
@ColumnInfo(name = "item2_id")
private String itemTwoId;
// THIS IS CAUSING THE ERROR... BASICALLY IT ISN'T READING ARRAYS
@ColumnInfo(name = "mylist_array")
private ArrayList<MyListItems> myListItems;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public ArrayList<MyListItems> getMyListItems() {
return myListItems;
}
public void setCheckListItems(ArrayList<MyListItems> myListItems) {
this.myListItems = myListItems;
}
}
所以基本上我想把ArrayList保存在数据库中,但我找不到任何与它相关的内容。您能指导我如何使用Room保存数组吗?
注意:MyListItems Pojo 类包含 2 个字符串(截至目前)
提前致谢。