본문 바로가기

PROGRAMING/Android

android.content.CursorLoader 와 LoaderCallbacks 인터페이스


CursorLoader
A loader that queries the ContentResolver and returns a Cursor. This class implements the Loader protocol in a standard way for querying cursors, building on AsyncTaskLoader to perform the cursor query on a background thread so that it does not block the application's UI.

A CursorLoader must be built with the full information for the query to perform, either through the CursorLoader(Context, Uri, String[], String, String[], String) or creating an empty instance with CursorLoader(Context) and filling in the desired paramters with setUri(Uri), setSelection(String), setSelectionArgs(String[]), setSortOrder(String), andsetProjection(String[]).


LoaderCallbacks

Callback interface for a client to interact with the manager.


public class MuiscList extends Activity implements LoaderCallbacks<Cursor>{



@Override

protected void onCreate(Bundle savedInstanceState) {

LOG.i(CLASSNAME,"onCreate");

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_muisc_list);

ListView listViewTmp = (ListView)findViewById(R.id.listView1);

mCursorAdapter = new CustomCursorAdapter(this);

listViewTmp.setAdapter(mCursorAdapter);

// Initializes the CursorLoader

getLoaderManager().initLoader(AUDIO_LOADER, null, this);

}


@Override

public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {

LOG.i(CLASSNAME,"onCreateLoader loaderID : "+loaderID);

switch (loaderID) {

case AUDIO_LOADER:

CursorLoader tmp = new CursorLoader(

this,                                     // Context

            audioExternalUri,  // Table to query

            null,                                        // Projection to return

            null,                                              // No selection clause

null,                                              // No selection arguments

null                                               // Default sort order

);

      if(null==tmp){

 LOG.i(CLASSNAME,"tmp is null");   

}

      return tmp;

default:

LOG.i(CLASSNAME,"onCreateLoader default");

return null;

}

}


@Override

public void onLoadFinished(Loader<Cursor> loader, Cursor returnCursor) {

LOG.i(CLASSNAME,"onLoadFinished");

mCursorAdapter.swapCursor(returnCursor);

LOG.i(CLASSNAME,"cursorAdapter.getCount() : "+mCursorAdapter.getCount());

}


@Override

public void onLoaderReset(Loader<Cursor> arg0) {

LOG.i(CLASSNAME,"onLoaderReset");

mCursorAdapter.swapCursor(null);

}