3

Android文档指出加载程序 - 它们监视其数据源并在内容更改时提供新结果。我已经改变了CursorAdapter为SQLite数据库工作。当源数据发生变化时,加载程序不加载数据

import android.content.ContentResolver; 
import android.content.Context; 
import android.database.ContentObserver; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v4.content.AsyncTaskLoader; 
import android.support.v4.content.Loader.ForceLoadContentObserver; 
import android.util.Log; 


public class SimpleCursorLoader extends AsyncTaskLoader<Cursor> { 

final ForceLoadContentObserver mObserver; 

String mTable; 
String[] mColumns; 
String mSelection; 
String[] mSelectionArgs; 
String mGroupBy; 
String mHaving; 
String mOrderBy; 
String mLimit; 

Cursor mCursor; 
SQLiteDatabase mDb; 
/** 
* Creates an empty unspecified CursorLoader. You must follow this with 
* calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc 
* to specify the query to perform. 
*/ 
public SimpleCursorLoader(Context context) { 
    super(context); 
    mObserver = new ForceLoadContentObserver(); 
} 

/** 
* Creates a fully-specified SimpleCursorLoader. See 
* {@link ContentResolver#query(Uri, String[], String, String[], String) 
* ContentResolver.query()} for documentation on the meaning of the 
* parameters. These will be passed as-is to that call. 
*/ 
public SimpleCursorLoader(Context context,String table, String[] columns, String  selection, 
     String[] selectionArgs, String groupBy, String having, String orderBy, String limit, SQLiteDatabase db) { 

    super(context); 
    mObserver = new ForceLoadContentObserver(); 
    mTable = table; 
    mColumns = columns; //copying array 
    mSelection = selection; 
    mSelectionArgs = selectionArgs; 
    mGroupBy = groupBy; 
    mHaving = having; 
    mOrderBy = orderBy; 
    mLimit = limit; 
    mDb = db; 
}  

    /* Runs on a worker thread */ 
@Override 
public Cursor loadInBackground() { 
    Cursor cursor = mDb.query(mTable, mColumns, mSelection, mSelectionArgs, mGroupBy, mHaving, mOrderBy, mLimit); 
    if (cursor != null) { 
     // Ensure the cursor window is filled 
     cursor.getCount(); 
     Log.d("SimpleCursorLoader","Cursor.getCount()= " + String.valueOf(cursor.getCount())); 
     registerContentObserver(cursor, mObserver); 
    } 
    return cursor; 
} 

/** 
* Registers an observer to get notifications from the content provider 
* when the cursor needs to be refreshed. 
*/ 
void registerContentObserver(Cursor cursor, ContentObserver observer) { 
    cursor.registerContentObserver(mObserver); 
} 

/* Runs on the UI thread */ 
@Override 
public void deliverResult(Cursor cursor) { 
    Log.d("Gaurav","Inside SimpleCursorLoader - deliverResult"); 
    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(); 
    } 
} 


/** 
* Starts an asynchronous load of the contacts list data. When the result is ready the callbacks 
* will be called on the UI thread. If a previous load has been completed and is still valid 
* the result may be passed to the callbacks immediately. 
* 
* Must be called from the UI thread 
*/ 
@Override 
protected void onStartLoading() { 
    Log.d("Gaurav", "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; 
} 

} 

的CursorLoader工作在与延伸SimpleCursor适配器并通过FLAG_REGISTER_CONTENT_OBSERVER在构造,但是当基础数据是通过在列表中使用的响应改变光标确实改变自定义适配器ListFragment。

+0

你的问题对我来说并不完全清楚。你是否说你的ListFragment在底层数据改变时不刷新? – codinguser

+0

@codinguser这是一个古老的问题,但这就是我的意思。 –

+1

@codingcrow你碰巧解决了这个问题吗? – Ads

回答

-6

您需要覆盖转储方法。

@Override 
    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 
     super.dump(prefix, fd, writer, args); 
     writer.print(prefix); writer.print("mTable="); writer.println(mTable); 
     writer.print(prefix); writer.print("mDB=");writer.println(mDb); 
     writer.print(prefix); writer.print("mProjection="); 
       writer.println(Arrays.toString(mProjection)); 
     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); 
    } 
0

我有同样的问题,尽管我使用的是与您不同的实现。我能够通过明确声明通知URI来更正我的问题。我确定我的代码还有其他问题,导致我需要这样做,但它解决了我的问题。所以,在我的内容提供商我的光标查询去从:

@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 

................ 
................ 
................ 

Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(), 
       projection, selection, selectionArgs, null, null, sortOrder); 
       cursor.setNotificationUri(getContext().getContentResolver(), uri); 
cursor.setNotificationUri(getContext().getContentResolver(), uri;) 

@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 

................ 
................ 
................ 

Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(), 
       projection, selection, selectionArgs, null, null, sortOrder); 
       cursor.setNotificationUri(getContext().getContentResolver(), uri); 
cursor.setNotificationUri(getContext().getContentResolver(), MYURI;) 

其中MYURI是,我知道会面面俱到我看URI类型的变量。

希望这可以帮助别人。