-
Notifications
You must be signed in to change notification settings - Fork 89
Custom provider
Ran Ben Aharon edited this page Aug 19, 2015
·
2 revisions
You can create your own model, based on your own content provider. Use annotations to map your table schema to your model. This will give you the same power of simplicity as do android content providers. In fact, android content providers are implemented in the same way.
dependencies {
compile 'me.everything:providers-core:1.0.0'
}
By using annotations we map model members to table columns and define some basic ORM mapping.
To make it work extend you model by Entity
class.
public class Post extends Entity {
...
}
And annotate your fields by using next options:
-
Ignore mapping
@IgnoreMapping private String someField; // field you want to ignore
-
Map Integer to int, Text to String,
@FieldMapping( columnName = "_id", physicalType = FieldMapping.PhysicalType.Int ) public int id; @FieldMapping( columnName = "title", physicalType = FieldMapping.PhysicalType.String ) public String title;
-
Map Blob to byte[]
@FieldMapping( columnName = "thumbnail", physicalType = FieldMapping.PhysicalType.Blob ) public byte[] thumbnail;
-
Map Integer to boolean
@FieldMapping( columnName = "is_owner", physicalType = FieldMapping.PhysicalType.Int, logicalType = FieldMapping.LogicalType.Boolean ) public boolean isOwner;
-
Map Integer to long
@FieldMapping( columnName = "updated_at", physicalType = FieldMapping.PhysicalType.Int, logicalType = FieldMapping.LogicalType.Long ) public long updatedAt;
-
Map Integer to Enum
@FieldMapping( columnName = "post_type", physicalType = FieldMapping.PhysicalType.Int, logicalType = FieldMapping.LogicalType.EnumInt ) public PostType postType; public enum PostType implements EnumInt { LINK(1), VIDEO(2); int val; PostType(int val) { this.val = val; } public static PostType fromVal(int val) { for (PostType postType : values()) { if (postType.val == val) { return postType; } } return null; } }