2011-09-09 7 views
0

1つのアクティビティで2つのListViewを使いたいと思います。私はフラグメントを使用しようとしています。データはSqliteデータベースからListViewにロードされます。 SQlliteで動作するAsyncTaskLoaderは、カーソルローダーのソースコードを使用していますが、メソッドdoInBackground()は間違いを起こします。これは私のコードです:AndroidはCursorLoaderをSQLiteデータベースローダーに実装します

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

誰かを助けることができます。なぜメソッドdoInBackground()がうまくいかないのですか?

答えて

1

クエリを実行する前に、データベースmDbを開く必要があります。