Ormlite With Cursoradapter In Android
Solution 1:
Your comments indicate that you've already answered you problem. You can certainly create a column named "_id" using ORMLite:
@DatabaseField(generatedId = true)privateint _id;
or
@DatabaseField(generatedId = true, columnName = "_id")
private intid;
If you are working with Cursor
s then you may want to take a look at the last()
and moveAbsolute(...)
methods on the DatabaseResults
class. Also, the AndroidDatabaseResults
(which you can cast to) also has a getRawCursor()
method which returns the underlying Cursor
object and has additional getCount()
and getPosition()
methods.
Here are some more information about ORMLite and Cursor
s:
You can get access to the Cursor
using something like the following:
// build your query
QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
qb.where()...;
// when you are done, prepare your query and build an iterator
CloseableIterator<Foo> iterator = dao.iterator(qb.prepare());
try {
// get the raw results which can be cast under Android
AndroidDatabaseResults results =
(AndroidDatabaseResults)iterator.getRawResults();
Cursor cursor = results.getRawCursor();
...
} finally {
iterator.closeQuietly();
}
Solution 2:
It turns out I did need a raw SQL query with ORMLite after all as I needed to do a Left Join, (i.e. not as a result of having to rename the id column in a query, that is not necessary as per Gray's answer above)
The way I did it is below:
publicclassDatabaseHelperextendsOrmLiteSqliteOpenHelper{
publicvoidmyRawQueryMethod() {
SQLiteDatabasedatabase=this.getReadableDatabase();
StringSqlQuery="Write raw SQL query here";
Cursorcursor= database.rawQuery(SqlQuery, null);
}
}
Thanks for advice
Solution 3:
Wrap your cursor with another one that treats "_id"
as an alias to your real primary key column. Example:
cursor = newCursorWrapper(cursor) {
@OverridepublicintgetColumnIndexOrThrow(String columnName)throws IllegalArgumentException {
if ("_id".equals(columnName)) columnName = "id";
returnsuper.getColumnIndexOrThrow(columnName);
}
};
Post a Comment for "Ormlite With Cursoradapter In Android"