android - 利用 ORMLite 将 Android Cursor 映射到 POJO?

标签 android cursor android-contentprovider ormlite

因为 ORMLite 已经将 Android Cursor 映射到 POJO。 我想知道我是否可以以某种方式利用它而不是编写复制构造函数并手动将 Cursor 映射到我的 POJO?

例如,我可以使用内容提供程序并将结果映射到由 ORMLite 的 Dao 支持的相同 POJO。 对我的数据集执行操作,然后使用 ORMLite 将该数据批量插入到我的数据库中。 这样做的好处是我不必编写所有的 CRUD ofc。

编辑:

这是一个捏造的例子,但概念在这里。执行此类操作的最佳方法是什么?

@DatabaseTable(tableName = "my_dict")
public class MyDictionary{
    @DatabaseField(columnName = COLUMN_ID, generatedId = true)
    Integer _ID;
    @DatabaseField(columnName = "word")
    String word;
    @DatabaseField(columnName = "app_id")
    String app_id;
    @DatabaseField(columnName = "frequency")
    Integer frequency;
    @DatabaseField(columnName = "locale")
    String locale;

    public MyDictionary(){}     
}

public void someCoolDatasetOperation(){

    Dao<MyDictionary, Long> dao = databaseHelper.getDao(MyDictionary.class);

    List<MyDictionary> inDb = dao.queryForAll();

    Cursor dictCursor = getContentResolver().query(WEB_SERVICE_URI,
            null,                        // The columns to return for each row
            null,                    // Selection criteria
            null,                     // Selection criteria
            null);                        // The sort order for the returned rows


    //iffy part don't know the correct/best way to do this?????
    AndroidDatabaseResults fabricatedResult = new AndroidDatabaseResults(dictCursor, null);     
    List<MyDictionary> fromContentProvider = dao.mapSelectStarRow(dictCursor);

    //Do Something with the lists
}

最佳答案

我对 ORMLite 了解不多,我不确定这两者会相处融洽,但我正在开发一个库,它可以像你那里那样使用带注释的类为游标生成 POJO 包装器。

因此,如果我尝试将此库与您上面的示例一起使用,它可能看起来像这样:

@Entity
public class MyDictionary{
    @Attribute(sqliteName = COLUMN_ID, primaryKey = true, autoIncrement = true)
    Integer _ID;
    @Attribute(sqliteName = "word")
    String word;
    @Attribute(sqliteName = "app_id")
    String app_id;
    @Attribute(sqliteName = "frequency")
    Integer frequency;
    @Attribute(sqliteName = "locale")
    String locale;
}

public void someCoolDatasetOperation(){

    Dao<MyDictionary, Long> dao = databaseHelper.getDao(MyDictionary.class);

    List<MyDictionary> inDb = dao.queryForAll();

    Cursor dictCursor = getContentResolver().query(WEB_SERVICE_URI,
            null,                    // The columns to return for each row
            null,                    // Selection criteria
            null,                    // Selection criteria
            null);                   // The sort order for the returned rows

    List<MyDictionary> fromContentProvider = new ArrayList<MyDictionary();

    while(dictCursor.moveToNext()) {
        //MyDictionaryEntity is a generated class that contains getters and 
        //setters for all the attributes in MyDictionary
        fromContentProvider.add(new MyDictionaryEntity(dictCursor));
    }
}

我仍在积极开发它,还没有找到太多文档,但如果您想查看它: https://github.com/adecker89/Glowplug

关于android - 利用 ORMLite 将 Android Cursor 映射到 POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753590/

相关文章:

java - 使用 Robolectric 测试启动 Activity 的代码

java - 安卓socket连接失败?

android - 更改操作栏搜索 View 提示文本颜色

jquery - 如何在 Wordpress 中的 NextGen Gallery 幻灯片上更改光标

Emacs - 为光标闪烁设置不同的时间间隔

android - 对非 SAF 内容的持久权限

android - 异步任务和守护进程之间的区别

android - java.lang.SecurityException : Permission Denial: reading 错误

android - 尝试在 Android CallLog.Calls 上获取日期时出错

python - 游标在 PyMySQL 中起什么作用和作用?