OrmLiteSimpleCursorAdapter

997 views
Skip to first unread message

Craig Andrews

unread,
Sep 12, 2011, 10:31:03 AM9/12/11
to ormlite...@googlegroups.com
Currently, when one wants to use ListView's or Spinner's with ORMLite
data, the only option is to create an ArrayAdapter. This option is pretty
bad, as it requires that all the data be loaded into memory at once, so if
there are a lot of rows, it's not only slow, but potentially uses more
memory than Android will allow.

Attached is my attempt at an adapter which uses an ORMLite PreparedQuery
to gather and display data for Spinner's and ListView's.

The class has 2 constructors, one which uses the PreparedQuery to get the
Android SQLite cursor, and one which takes the cursor as an argument. In
the first case, since the adapter is constructed in the UI thread, the
cursor is also retrieved in the UI thread, which means the PreparedQuery
is executed on the UI thread... which is definitely not good. However, the
second constructor is harder/messier to use. I don't know what the clean
solution is.

I'd love to receive some feedback on this approach, and hopefully get it
included in ORMLite-Android soon :-)

Thanks,
~Craig

OrmLiteSimpleCursorAdapter.java

Mark Andrachek, Jr.

unread,
Sep 22, 2011, 2:08:06 PM9/22/11
to ormlite-android
I have a very similar implementation. However I went with using the
new Loader framework (part of Honeycomb - I used the static support
library, however), as it takes care of the UI thread issues for you.

I've got an ORMLoader<T> that extends AsycTaskLoader<Cursor>. This
gets the Activity, my OrmLiteSqliteOpenHelper implementation, and my
PreparedQuery<T> as arguments to the constructor. The
loadInBackground() method uses these to pull the cursor, eg.
Cursor cursor =
((AndroidCompiledStatement)mQuery.compile(mDatabaseHelper.getConnectionSource().getReadOnlyConnection())).getCursor();

Then I've got my ORMQueryAdapter<T>, which extends
ResourceCursorAdapter. This gets the Context, the layout id, the
PreparedQuery<T>, and the ViewBinder<T> as constructor params. Then in
the overridden bindView method, I do the mQuery.mapRow() bit.

Thanks,
Mark Andrachek
mark.an...@gmail.com

On Sep 12, 10:31 am, "Craig Andrews" <candr...@integralblue.com>
wrote:
>  OrmLiteSimpleCursorAdapter.java
> 3KViewDownload
Reply all
Reply to author
Forward
0 new messages