1 page.title=Handling the Results 2 trainingnavtop=true 3 startpage=true 4 5 @jd:body 6 7 <!-- This is the training bar --> 8 <div id="tb-wrapper"> 9 <div id="tb"> 10 <h2>This lesson teaches you to</h2> 11 <ol> 12 <li> 13 <a href="#HandleResults">Handle Query Results</a> 14 </li> 15 <li> 16 <a href="#HandleReset">Clear Out Old Data</a></li> 17 </ol> 18 </div> 19 </div> 20 21 <p> 22 {@link android.support.v4.content.CursorLoader} returns its query results to your 23 implementation of 24 {@link android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished 25 LoaderCallbacks.onLoadFinished()}, in the form of a {@link android.database.Cursor}. In the 26 callback, you can update your data display, do further processing on the 27 {@link android.database.Cursor} data, and so forth. 28 </p> 29 <p> 30 When the loader framework detects changes to data associated with the query, 31 it resets the {@link android.support.v4.content.CursorLoader}, closes the current 32 {@link android.database.Cursor}, and then invokes your implementation of 33 {@link android.support.v4.app.LoaderManager.LoaderCallbacks#onLoaderReset onLoaderReset()}. 34 Use this callback to delete references to the current {@link android.database.Cursor}; when the 35 loader framework destroys the {@link android.database.Cursor}, you won't have outstanding 36 references that cause memory leaks. 37 </p> 38 <h2 id="HandleFinished">Handle Query Results</h2> 39 <p> 40 The following two snippets are an example of displaying the results of a query, using a 41 {@link android.widget.ListView} backed by a 42 {@link android.support.v4.widget.SimpleCursorAdapter}. 43 </p> 44 <p> 45 The first snippet shows the {@link android.widget.ListView} and 46 {@link android.support.v4.widget.SimpleCursorAdapter}: 47 </p> 48 <pre> 49 // Gets a handle to the Android built-in ListView widget 50 mListView = ((ListView) findViewById(android.R.id.list)); 51 // Creates a CursorAdapter 52 mAdapter = 53 new SimpleCursorAdapter( 54 this, // Current context 55 R.layout.logitem, // View for each item in the list 56 null, // Don't provide the cursor yet 57 FROM_COLUMNS, // List of cursor columns to display 58 TO_FIELDS, // List of TextViews in each line 59 0 // flags 60 ); 61 // Links the adapter to the ListView 62 mListView.setAdapter(mAdapter); 63 </pre> 64 <p> 65 The next snippet shows an implementation of 66 {@link android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished onLoadFinished()} 67 that moves the query results in the returned {@link android.database.Cursor} to the 68 {@link android.support.v4.widget.SimpleCursorAdapter}. Changing the 69 {@link android.database.Cursor} in the 70 {@link android.support.v4.widget.SimpleCursorAdapter} triggers a refresh of the 71 {@link android.widget.ListView} with the new data: 72 </p> 73 <pre> 74 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) 75 { 76 /* 77 * Move the results into the adapter. This 78 * triggers the ListView to re-display. 79 */ 80 mAdapter.swapCursor(cursor); 81 } 82 </pre> 83 <h2 id="HandleReset">Handle a Loader Reset</h2> 84 <p> 85 The loader framework resets the {@link android.support.v4.content.CursorLoader} whenever the 86 {@link android.database.Cursor} becomes invalid. This usually occurs because the data associated 87 with the {@link android.database.Cursor} has changed. Before re-running the query, 88 the framework calls your implementation of 89 {@link android.support.v4.app.LoaderManager.LoaderCallbacks#onLoaderReset onLoaderReset()}. In 90 this callback, make sure to prevent memory leaks by deleting all references to the current 91 {@link android.database.Cursor}. Once you return from 92 {@link android.support.v4.app.LoaderManager.LoaderCallbacks#onLoaderReset onLoaderReset()}, 93 the loader framework re-runs the query. 94 </p> 95 <p> 96 For example: 97 </p> 98 <pre> 99 public void onLoaderReset(Loader<Cursor> loader) 100 { 101 // Remove the reference to the current Cursor 102 mAdapter.swapCursor(null); 103 } 104 </pre> 105