Android implement CursorLoader to SQLite database loader
I want to have two ListView in one Activity. I am trying to use Fragments. Data will loaded to ListViews from Sqlite database. I implement AsyncTaskLoader for working with SQllite used source code of cursorloader, but method doInBackground() issue mistake. This is my code:
public class DatabaseLoader extends AsyncTaskLoader<Cursor> {
final ForceLoadContentObserver mObserver;
SQLiteDatabase mDb;
String mTable;
String[] mColumns;
String mSelection;
String[] mSelectionArgs;
String mSortOrder;
Cursor mCursor;
/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
Cursor cursor = mDb.query(mTable, mColumns, mSelection,
mSelectionArgs, null, null, mSortOrder, null);
if (cursor != null) {
cursor.getCount();
registerContentObserver(cursor, mObserver);
}
return cursor;
}
void registerContentObserver(Cursor cursor, ContentObserver observer) {
cursor.registerContentObserver(mObserver);
}
/* Runs on the UI thread */
@Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
oldCursor.close();
}
}
public DatabaseLoader(Context context) {
super(context);
mObserver = new ForceLoadContentObserver();
}
public DatabaseLoader(Context context, String table, String[] columns, String selection,
String[] selectionArgs, String sortOrder) {
super(context);
mObserver = new ForceLoadContentObserver();
mTable = table;
mColumns = columns;
mSelection = selection;
mSelectionArgs = selectionArgs;
mSortOrder = sortOrder;
}
@Override
protected void onStartLoading() {
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
/**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
public void onCanceled(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
public String getTable() {
return mTable;
}
public void setT开发者_如何学Cable(String table) {
mTable = table;
}
public String[] getColumns() {
return mColumns;
}
public void setColumns(String[] columns) {
mColumns = columns;
}
public String getSelection() {
return mSelection;
}
public void setSelection(String selection) {
mSelection = selection;
}
public String[] getSelectionArgs() {
return mSelectionArgs;
}
public void setSelectionArgs(String[] selectionArgs) {
mSelectionArgs = selectionArgs;
}
public String getSortOrder() {
return mSortOrder;
}
public void setSortOrder(String sortOrder) {
mSortOrder = sortOrder;
}
@Override
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
super.dump(prefix, fd, writer, args);
// writer.print(prefix); writer.print("mUri="); writer.println(mUri);
writer.print(prefix); writer.print("mTable="); writer.println(mTable);
writer.print(prefix); writer.print("mColumns="); writer.println(Arrays.toString(mColumns));
writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
writer.print(prefix); writer.print("mSelectionArgs="); writer.println(Arrays.toString(mSelectionArgs));
writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
// writer.print(prefix); writer.print("mContentChanged=");
writer.println(mContentChanged);
}
}
Can help me somebody. Why method doInBackground() don't work?
You have to open the Database mDb before performing query on it.
精彩评论