自动生成内容提供者
示意图 ========== 自动生成由 SQLite 数据库支持的 ContentProvider。 使用 ===== 首先创建一个包含数据库表列的类。 java public interface ListColumns { @DataType(INTEGER) @PrimaryKey @AutoIncrement String ID = "id"; @DataType(TEXT) @NotNull String TITLE = "title"; } 然后创建一个使用此列的数据库 java @Database(version = NotesDatabase.VERSION) public final class NotesDatabase { public static final int VERSION = 1; @Table(ListColumns.class) public static final String LISTS = "lists"; } 最后定义一个 ContentProvider java @ContentProvider(authority = NotesProvider.AUTHORITY, database = NotesDatabase.class) public final class NotesProvider { public static final String AUTHORITY = "net.simonvt.schematic.sample.NotesProvider"; @TableEndpoint(table = NotesDatabase.LISTS) public static class Lists { @ContentUri( path = "lists", type = "vnd.android.Cursor.dir/list", defaultSort = ListColumns.TITLE + " ASC") public static final Uri LISTS = Uri.parse("content://" + AUTHORITY + "/lists"); } 表列注解 ------------------------ java @AutoIncrement @DataType @DefaultValue @NotNull @PrimaryKey @References @Unique 定义 Uri --------------- @ContentUri 注解用于当 Uri 不变时。 java @ContentUri( path = "lists", type = "vnd.android.Cursor.dir/list", defaultSort = ListColumns.TITLE + " ASC") public static final Uri LISTS = Uri.parse("content://" + AUTHORITY + "/lists"); 如果 Uri 根据某个值创建,例如 ID,则使用 @InexactContentUri。 java @InexactContentUri( path = Path.LISTS + "/#", name = "LISTID", type = "vnd.android.Cursor.item/list", whereColumn = ListColumns.ID, pathSegment = 1) public static Uri withId(long id) { return Uri.parse("content://" + AUTHORITY + "/lists/" + id); } 加入项目 ========================= groovy dependencies { annotationProcessor 'net.simonvt.schematic:schematic-compiler:{latest-version}' compile 'net.simonvt.schematic:schematic:{latest-version}' } 许可 =========== Copyright 2014 Simon Vig Therkildsen 根据 Apache 许可证,版本 2.0("许可";您不得使用本文件,除非…
暂无开放 Issues,或尚未同步最近议题。