package com.example.ormlite.cursor; import android.content.AsyncTaskLoader; import android.content.Context; import android.database.Cursor; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.support.DatabaseConnection; import com.j256.ormlite.android.AndroidCompiledStatement import java.sql.SQLException; import static com.j256.ormlite.stmt.StatementBuilder.StatementType.SELECT; /** * Cursor loader supported by later Android APIs that allows asynchronous content loading. * Edited by andrei on 26/15/15 * @author emmby */ public class OrmLiteCursorLoader extends AsyncTaskLoader { protected Dao dao; protected PreparedQuery query; protected Cursor cursor; public OrmLiteCursorLoader(Context context, Dao dao, PreparedQuery query) { super(context); this.dao = dao; this.query = query; // dao.registerObserver(this); } @Override public Cursor loadInBackground() { Cursor cursor; try { DatabaseConnection connection = dao.getConnectionSource().getReadOnlyConnection(); cursor = ((AndroidCompiledStatement) query.compile(connection, SELECT)).getCursor(); } catch (SQLException e) { throw new RuntimeException(e); } // fill the cursor with results cursor.getCount(); return cursor; } @Override public void deliverResult(Cursor newCursor) { if (isReset()) { // an async query came in while the loader is stopped if (newCursor != null) { newCursor.close(); } return; } Cursor oldCursor = cursor; cursor = newCursor; if (isStarted()) { super.deliverResult(newCursor); } // close the old cursor if necessary if (oldCursor != null && oldCursor != newCursor && !oldCursor.isClosed()) { oldCursor.close(); } } @Override protected void onStartLoading() { if (cursor == null) { forceLoad(); } else { deliverResult(cursor); if (takeContentChanged()) { forceLoad(); } } } @Override protected void onStopLoading() { cancelLoad(); } @Override public void onCanceled(Cursor cursor) { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } @Override protected void onReset() { super.onReset(); onStopLoading(); if (cursor != null) { if (!cursor.isClosed()) { cursor.close(); } cursor = null; } } /* You'll have to handle onChange() yourself since I removed the DaoObserver interface for it to be compatible with OrmLite 4.48 */ public void onChange() { onContentChanged(); } public PreparedQuery getQuery() { return query; } public void setQuery(PreparedQuery mQuery) { this.query = mQuery; } }