1.实现方式
主要是通过greenDao 提供的PropertyConverter,gson,转换对象到字符串存到数据库。
2.Converent实现
public class MyObjectConverent implements PropertyConverter<List<MyObjectItem>,String> {
private final Gson mGson;
public FreeCuttingCLJCConverent() {
mGson = new Gson();
}
@Override
public List<MyObjectItem> convertToEntityProperty(String databaseValue) {
Type type = new TypeToken<ArrayList<MyObjectItem>>() {
}.getType();
ArrayList<MyObjectItem> itemList= mGson.fromJson(databaseValue, type);
return itemList;
}
@Override
public String convertToDatabaseValue(List<MyObjectItem> entityProperty) {
String dbString = mGson.toJson(entityProperty);
return dbString;
}
}
2.ORM实体实现
@Entity
public class MyObjectEntity{
@Id
public Long tableId;
@Convert(/**指定转换器 **/converter = MyObjectConverent .class,/**指定数据库中的列字段**/columnType =String.class )
private List<MyObjectItem> mObjectList;
}
3.使用
//伪代码
//存的时候
MyObjectEntity.setObjectItem( new MyObjectItem());
//取的时候
MyObjectEntity.getObjectItem();
//就这么简单!!
本文介绍如何利用GreenDao的PropertyConverter与Gson库相结合,实现将对象列表转换为字符串存储于数据库的方法。通过自定义转换器MyObjectConverent,实现了List<MyObjectItem>类型的对象与字符串之间的相互转换。

700

被折叠的 条评论
为什么被折叠?



