Sometimes there are some attributes in our entity classes, but there are no corresponding types in the database, and we need custom converters for type conversions. The most common treatment is the date type. If the accuracy requirement is not high, we will convert to a long type to store, read the time to say that it is converted to the date type. So if we want it to be stored directly in a time string, this is a good thing, Greendao provides us with support for custom type conversions.
First, we need to implement the Propertyconverter interface to implement the two methods inside the converttoentityproperty and Converttodatabasevalue , the interface requires two generic parameters, the first parameter is the type in the entity class, and the second parameter is the type stored in the database. Now suppose we need to convert the date to a string for storage, so the implementation of the interface should be like this.
publicclass DateStringConverter implements PropertyConverter<Date,String> { @Override public Date convertToEntityProperty(String databaseValue) { returnnull; } @Override public String convertToDatabaseValue(Date entityProperty) { return bull; }}
And then we're going to convert
Public class datestringconverter implements Propertyconverter<Date, String> { Private Static FinalString default_format="Yyyy-mm-dd HH:mm:ss"; @Override PublicDate Converttoentityproperty (String databasevalue) {returnConvert2date (Databasevalue,default_format); } @Override PublicString Converttodatabasevalue (Date entityproperty) {returnConvert2string (Entityproperty,default_format); } Public StaticString convert2string (DateDate, string format) {string currentdate=NULL;Try{SimpleDateFormat formatter=NewSimpleDateFormat (format); Currentdate=formatter.format (Date); }Catch(Exception e) {E.printstacktrace (); }returncurrentdate; } Public StaticDate convert2date (String day, string format) {if(Day = =NULL|| Format = =NULL)return NULL; SimpleDateFormat formatter =NewSimpleDateFormat (format);Try{Date dt = Formatter.parse (day);returnDt }Catch(ParseException e) {E.printstacktrace (); }return NULL; }}
The entity class is then generated
type = schema.addEntity("Demo");type.addStringProperty("test").customType("java.util.Date","cn.edu.zafu.greendao.db.converter.DateStringConverter");
AddProperty is the corresponding type in the database, where we store it as a string, so it is addstringproperty, and then through the customtype function to specify the type of the entity class, here is Java.util.Date, followed by the full class name of our type converter. Then try inserting a piece of data into the database, and we'll find that he's storing it directly in a string,
Finally, a confusing configuration is included to add the following statement to the item that needs to be confused
class * extends de.greenrobot.dao.AbstractDao { publicstaticclass **$Properties
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android ORM Series Greendao custom type converter with code obfuscation configuration