Convert results of SQLite Cursor to my object

自古美人都是妖i 提交于 2019-11-30 13:10:56

I don't think there is an automated way to do this. Just populate the object yourself. For example, if your cursor was just an id and a name and you wanted to create a Person object:

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();
    }
}

You could wrap this function in your own Cursor class to make the process transparent.

Check out MicroOrm

private static class SomeObject {
  @Column(SOME_FIELD)
  private String mSomeField;
}

SomeObject object = microOrm.fromCursor(cursor, SomeObject.class);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!