3

Android 설명서에 따르면 LOADERS - 콘텐츠 소스가 변경 될 때 데이터 소스를 모니터링하고 새로운 결과를 제공합니다. 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에 명시 적으로 통지 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;) 

MYURI은 내가 지켜보고있어 모든 것을 다룰 것입니다 알고있는 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;) 

에 : 그래서, 내 콘텐츠 제공 업체에서 내 커서 쿼리에서 갔다.

누군가가 도움이되기를 바랍니다.