Ale*_*lex 9 android cursor pojo gson
我在Android上的SQLite DB上执行了一些查询.
使用的主要指令是:
Cursor cursor = myDB.rawQuery(select, null);
Run Code Online (Sandbox Code Playgroud)
现在我希望将此查询的结果转换为我在项目中创建的泛型类.如果您认为我想要一个ORM系统,那么您是对的,但我现在发现的是ORM系统,它们想要查询数据库,保存数据库中的对象并管理数据库本身.
相反,现在我需要一个"简单"的ORM功能,它可以完全执行google.gson库对JSON字符串的操作,它将JSON字符串转换为自定义对象,我想要相同但是将SQLite游标转换为我的类.
有什么建议怎么做?
Sam*_*uel 13
我认为没有一种自动化的方法可以做到这一点.只需自己填充对象.例如,如果您的光标只是一个id和一个名称,并且您想要创建一个Person对象:
Person populatePerson(Cursor cursor)
{
try
{
int idIndex = cursor.getColumnIndexOrThrow("_id");
int nameIndex = cursor.getColumnIndexOrThrow("name");
long id = cursor.getLong(idIndex);
String name = cursor.getString(nameIndex);
return new Person(id, name);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将此函数包装在您自己的Cursor类中,以使该过程透明.
查看MicroOrm
private static class SomeObject {
@Column(SOME_FIELD)
private String mSomeField;
}
SomeObject object = microOrm.fromCursor(cursor, SomeObject.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15612 次 |
| 最近记录: |