gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:07:14 24 4
gpt4 key购买 nike

因为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com