Android Cursor with Ormlite to Use in Cursoradapter

Android Cursor with ORMLite to use in CursorAdapter

ORMLite now supports next(), previous(), moveRelative(offset), ... methods on the CloseableIterator class. This should allow you to move the underlying Cursor object around at will.

It also supports the following DAO Cursor methods:

  • dao.mapSelectStarRow(databaseResults) Return the latest row from the database results from a query to select *. With this you can change the cursor location (for example) and then get the current object.
  • dao.getSelectStarRowMapper() Provides a mapper that you can use to map the object outside of the Dao.

When you are building your own query with ORMLite, you use the QueryBuilder object. queryBuilder.prepare() returns a PreparedQuery which is used by various methods in the DAO. You can call dao.iterator(preparedQuery) which will return a CloseableIterator which is used to iterate through the results. There is a iterator.getRawResults() to get access to the DatabaseResults class. Under Android, this can be cast to an AndroidDatabaseResults which has a getCursor() method on it to return the Android Cursor.

Something like the following code:

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

This is a bit complicated but you are definitely having to peer behind the vale to get to this object which is hidden by the database abstraction classes.

ORMLite with CursorAdapter in Android

Your comments indicate that you've already answered you problem. You can certainly create a column named "_id" using ORMLite:

@DatabaseField(generatedId = true)
private int _id;

or

@DatabaseField(generatedId = true, columnName = "_id")
private int id;

If you are working with Cursors 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 Cursors:

Android Cursor with ORMLite to use in CursorAdapter

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

Get a cursor with a raw sql with ormlite

Well I just found a solution which seems to be efficient, simple, and compliant with ormlite.

I just have to get an AndroidDatabase with getHelper().getReadableDatabase().

and then use

Cursor cursor = db.query("choixpointverification",
new String[] { "id", "id as _id", "nom" },
"masque = 0 and idPointVerification = " + idPointVerification.toString(),
null, null, null, "tri");

Problems getting Android Cursor from an DAOObject with ORMLite

when I try to do: "iterator.getRawResults();" well, the iterator class don't recognize the getRawResults method.

I'm confused by this. CloseableIterator has supported this method since version 4.30. You may need to upgrade to a more recent version.

AndroidDatabaseResults results =
(AndroidDatabaseResults)iterator.getRawResults();
Cursor cursor = results.getRawCursor();

You say that this is a failed attempt but the code is correct. Please let me know what exceptions you are getting or what problems you are seeing and I can address them.

ORMLite performance: ArrayAdapter vs CursorAdapter vs a custom adapter

Its better to use a custom adapter with
Content provider.

ORM Lite with ListView and Mysql backend (android)

  1. Your particular problem is trying to cast DatabaseResults
    interface to AndroidDatabaseResults hoping that it's workable -
    result shows that it's not
  2. Your fundamental problem is trying to
    extract Cursor from Iterator which is also unreliable.

You should get Cursor from ContentProvider which has to be loaded using LoaderManager and your query should be placed where query() method of ContentProvider sits.



Related Topics



Leave a reply



Submit