Home | History | Annotate | Download | only in components
      1 page.title=
      2 parent.title=
      3 parent.link=activities.html
      4 @jd:body
      5 <div id="qv-wrapper">
      6 <div id="qv">
      7     <h2>  </h2>
      8     <ol>
      9     <li><a href="#summary"> API </a></li>
     10     <li><a href="#app">   </a>
     11       <ol>
     12         <li><a href="#requirements"></a></li>
     13         <li><a href="#starting"> </a></li>
     14         <li><a href="#restarting">  </a></li>
     15         <li><a href="#callback">LoaderManager  </a></li>
     16       </ol>
     17     </li>
     18     <li><a href="#example"></a>
     19        <ol>
     20          <li><a href="#more_examples"> </a></li>
     21         </ol>
     22     </li>
     23   </ol>
     24 
     25   <h2>Key </h2>
     26     <ol>
     27       <li>{@link android.app.LoaderManager}</li>
     28       <li>{@link android.content.Loader}</li>
     29 
     30     </ol>
     31 
     32     <h2> </h2>
     33    <ol>
     34      <li> <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderCursor.html">
     35 LoaderCursor</a></li>
     36      <li> <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html">
     37 LoaderThrottle</a></li>
     38    </ol>
     39   </div>
     40 </div>
     41 
     42 <p> Android 3.0  ,      
     43    .    .</p>
     44   <ul>
     45     <li> {@link android.app.Activity} {@link
     46 android.app.Fragment}   .</li>
     47     <li>   .</li>
     48     <li>       
     49 .</li>
     50     <li>    ,    
     51  .     
     52 .</li>
     53   </ul>
     54 
     55 <h2 id="summary"> API </h2>
     56 
     57 <p>       
     58   .    .</p>
     59 
     60 <table>
     61   <tr>
     62     <th>/</th>
     63     <th></th>
     64   </tr>
     65   <tr>
     66     <td>{@link android.app.LoaderManager}</td>
     67     <td>{@link android.app.Activity} 
     68 {@link android.app.Fragment}   ,   {@link
     69 android.content.Loader}    .  
     70 {@link android.app.Activity}
     71   {@link android.app.Fragment}           .    
     72 {@link android.content.CursorLoader}   ,
     73            .
     74     <br />
     75     <br />
     76        {@link android.app.LoaderManager}  .  {@link android.app.LoaderManager}    
     77 .</td>
     78   </tr>
     79   <tr>
     80     <td>{@link android.app.LoaderManager.LoaderCallbacks}</td>
     81     <td>  {@link
     82 android.app.LoaderManager}     .   {@link
     83 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()}
     84        .</td>
     85   </tr>
     86   <tr>
     87     <td>{@link android.content.Loader}</td>
     88     <td>     .   
     89 .  {@link
     90 android.content.CursorLoader}  ,     .    
     91        
     92   . </td>
     93   </tr>
     94   <tr>
     95     <td>{@link android.content.AsyncTaskLoader}</td>
     96     <td>  {@link android.os.AsyncTask}   .</td>
     97   </tr>
     98   <tr>
     99     <td>{@link android.content.CursorLoader}</td>
    100     <td>{@link android.content.AsyncTaskLoader}  , 
    101 {@link android.content.ContentResolver}  {@link
    102 android.database.Cursor} .        {@link
    103 android.content.Loader}  ,
    104 {@link android.content.AsyncTaskLoader} 
    105       UI  .
    106       API      {@link
    107 android.content.ContentProvider}
    108     .</td>
    109   </tr>
    110 </table>
    111 
    112 <p>         
    113    .  
    114       ,    {@link
    115 android.app.LoaderManager}   {@link
    116 android.content.CursorLoader}  {@link android.content.Loader}
    117   .       
    118     .</p>
    119 
    120 <h2 id ="app">   </h2>
    121 <p>  Android      . 
    122      .</p>
    123 <ul>
    124   <li>{@link android.app.Activity}  {@link android.app.Fragment}.</li>
    125   <li>{@link android.app.LoaderManager} .</li>
    126   <li>{@link
    127 android.content.ContentProvider}    {@link android.content.CursorLoader}. ,  
    128 {@link android.content.Loader}  {@link android.content.AsyncTaskLoader}   
    129     .</li>
    130   <li>{@link android.app.LoaderManager.LoaderCallbacks} .
    131        
    132 .</li>
    133 <li>   (: {@link
    134 android.widget.SimpleCursorAdapter})</li>
    135   <li>{@link android.content.ContentProvider}   ,
    136 {@link android.content.CursorLoader}   .</li>
    137 </ul>
    138 <h3 id="starting"> </h3>
    139 
    140 <p>{@link android.app.LoaderManager} {@link android.app.Activity} 
    141 {@link android.app.Fragment}    {@link
    142 android.content.Loader}  .    {@link
    143 android.app.LoaderManager}  .</p>
    144 
    145 <p>
    146  {@link
    147 android.app.Activity#onCreate onCreate()}  ,  
    148 {@link android.app.Fragment#onActivityCreated onActivityCreated()}   {@link android.content.Loader} .  
    149    .</p>
    150 
    151 <pre>// Prepare the loader.  Either re-connect with an existing one,
    152 // or start a new one.
    153 getLoaderManager().initLoader(0, null, this);</pre>
    154 
    155 <p>{@link android.app.LoaderManager#initLoader initLoader()} 
    156    .</p>
    157 <ul>
    158   <li>   ID.   ID 0.</li>
    159 <li>     
    160 (  <code>null</code>).</li>
    161 
    162 <li>{@link android.app.LoaderManager.LoaderCallbacks} .    
    163 {@link android.app.LoaderManager}  .  
    164    {@link
    165 android.app.LoaderManager.LoaderCallbacks}     
    166 {@code this} .</li>
    167 </ul>
    168 <p>{@link android.app.LoaderManager#initLoader initLoader()}     
    169  .        .</p>
    170 <ul>
    171   <li>ID     ,   
    172 .</li>
    173   <li>ID    <em></em> ,
    174 {@link android.app.LoaderManager#initLoader initLoader()}
    175 {@link android.app.LoaderManager.LoaderCallbacks}  {@link android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} .
    176       .
    177   <a href="#onCreateLoader">onCreateLoader</a>  .</li>
    178 </ul>
    179 <p>   {@link android.app.LoaderManager.LoaderCallbacks}
    180     ,   
    181  .      
    182         
    183   ,   {@link
    184 android.app.LoaderManager.LoaderCallbacks#onLoadFinished onLoadFinished()}
    185 ({@link android.app.LoaderManager#initLoader initLoader()} ).
    186       .      <a href="#onLoadFinished">
    187 onLoadFinished</a> .</p>
    188 
    189 <p>{@link android.app.LoaderManager#initLoader initLoader()}
    190   {@link android.content.Loader} ,       
    191 . {@link android.app.LoaderManager}  
    192  . {@link android.app.LoaderManager}
    193     ,     
    194 .    ,  
    195     (,          
    196 <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html"> LoaderThrottle</a>   ).
    197     {@link
    198 android.app.LoaderManager.LoaderCallbacks},      
    199    .      <a href="#callback">LoaderManager  </a> .</p>
    200 
    201 <h3 id="restarting">  </h3>
    202 
    203 <p>    {@link android.app.LoaderManager#initLoader initLoader()}  ,
    204   ID      .
    205 ,  .         
    206 .</p>
    207 
    208 <p>   {@link
    209 android.app.LoaderManager#restartLoader restartLoader()} .  , 
    210 {@link android.widget.SearchView.OnQueryTextListener} 
    211      .       
    212     .</p>
    213 
    214 <pre>
    215 public boolean onQueryTextChanged(String newText) {
    216     // Called when the action bar search text has changed.  Update
    217     // the search filter, and restart the loader to do a new query
    218     // with this filter.
    219     mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
    220     getLoaderManager().restartLoader(0, null, this);
    221     return true;
    222 }</pre>
    223 
    224 <h3 id="callback">LoaderManager  </h3>
    225 
    226 <p>{@link android.app.LoaderManager.LoaderCallbacks} 
    227 {@link android.app.LoaderManager}       . </p>
    228 <p>,  {@link android.content.CursorLoader}  
    229     .   
    230    @link android.app.Activity#onStop
    231 onStop()}  {@link android.app.Activity#onStart onStart()}     ,
    232        
    233  .       {@link android.app.LoaderManager.LoaderCallbacks}
    234  .       
    235        .</p>
    236 
    237 <p>{@link android.app.LoaderManager.LoaderCallbacks}   
    238 .</p>
    239 <ul>
    240   <li>{@link android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} -
    241  ID    {@link android.content.Loader} .
    242 </li></ul>
    243 <ul>
    244   <li> {@link android.app.LoaderManager.LoaderCallbacks#onLoadFinished onLoadFinished()}
    245 -      .
    246 </li></ul>
    247 <ul>
    248   <li>{@link android.app.LoaderManager.LoaderCallbacks#onLoaderReset onLoaderReset()}
    249 -           
    250 .
    251 </li>
    252 </ul>
    253 <p>       .</p>
    254 
    255 <h4 id ="onCreateLoader">onCreateLoader</h4>
    256 
    257 <p>   (  {@link
    258 android.app.LoaderManager#initLoader initLoader()} ),   ID   
    259  .   , {@link
    260 android.app.LoaderManager.LoaderCallbacks}  {@link
    261 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} . 
    262   .   {@link
    263 android.content.CursorLoader},   {@link
    264 android.content.Loader}    . </p>
    265 
    266 <p> , {@link
    267 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()}
    268    {@link android.content.CursorLoader} .
    269 {@link android.content.CursorLoader}      . 
    270 {@link
    271 android.content.ContentProvider}         .     .</p>
    272 <ul>
    273   <li><em>uri</em>    URI. </li>
    274   <li><em></em>    .
    275 <code>null</code>    ,  . </li>
    276   <li><em></em>     ,
    277 SQL WHERE   (WHERE  ).
    278 <code>null</code>   URI    . </li>
    279   <li><em>selectionArgs</em>   ?  .  
    280  <em>selectionArgs</em>   ,   
    281  .   . </li>
    282   <li><em>sortOrder</em>  SQL ORDER BY   
    283    (ORDER BY  ). <code>null</code>
    284     ,   .</li>
    285 </ul>
    286 <p>:</p>
    287 <pre>
    288  // If non-null, this is the current filter the user has provided.
    289 String mCurFilter;
    290 ...
    291 public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) {
    292     // This is called when a new Loader needs to be created. This
    293     // sample only has one Loader, so we don't care about the ID.
    294     // First, pick the base URI to use depending on whether we are
    295     // currently filtering.
    296     Uri baseUri;
    297   if (mCurFilter != null) {
    298     baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
    299           Uri.encode(mCurFilter));
    300   } else {
    301     baseUri = Contacts.CONTENT_URI;
    302   }
    303 
    304   // Now create and return a CursorLoader that will take care of
    305   // creating a Cursor for the data being displayed.
    306   String select = &quot;((&quot; + Contacts.DISPLAY_NAME + &quot; NOTNULL) AND (&quot;
    307       + Contacts.HAS_PHONE_NUMBER + &quot;=1) AND (&quot;
    308       + Contacts.DISPLAY_NAME + &quot; != '' ))&quot;;
    309   return new CursorLoader(getActivity(), baseUri,
    310       CONTACTS_SUMMARY_PROJECTION, select, null,
    311       Contacts.DISPLAY_NAME + &quot; COLLATE LOCALIZED ASC&quot;);
    312 }</pre>
    313 <h4 id="onLoadFinished">onLoadFinished</h4>
    314 
    315 <p>       .
    316         
    317   .     
    318     (  ),      .  
    319  ,     .</p>
    320 
    321 
    322 <p>            
    323  .     {@link
    324 android.content.CursorLoader}  ,   {@link
    325 android.database.Cursor#close close()}   . 
    326 {@link android.widget.CursorAdapter}  , {@link
    327 android.widget.SimpleCursorAdapter#swapCursor swapCursor()}   . 
    328  {@link android.database.Cursor}  . :</p>
    329 
    330 <pre>
    331 // This is the Adapter being used to display the list's data.<br
    332 />SimpleCursorAdapter mAdapter;
    333 ...
    334 
    335 public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
    336     // Swap the new cursor in. (The framework will take care of closing the
    337     // old cursor once we return.)
    338     mAdapter.swapCursor(data);
    339 }</pre>
    340 
    341 <h4 id="onLoaderReset">onLoaderReset</h4>
    342 
    343 <p>            
    344 .         
    345       . </p>
    346 <p> 
    347 {@link android.widget.SimpleCursorAdapter#swapCursor swapCursor()} ,
    348   <code>null</code> .</p>
    349 
    350 <pre>
    351 // This is the Adapter being used to display the list's data.
    352 SimpleCursorAdapter mAdapter;
    353 ...
    354 
    355 public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
    356     // This is called when the last Cursor provided to onLoadFinished()
    357     // above is about to be closed. We need to make sure we are no
    358     // longer using it.
    359     mAdapter.swapCursor(null);
    360 }</pre>
    361 
    362 
    363 <h2 id="example"></h2>
    364 
    365 <p>    {@link android.widget.ListView}  {@link
    366 android.app.Fragment}   . 
    367        .  {@link
    368 android.content.CursorLoader}     .</p>
    369 
    370 <p>        
    371  
    372 {@link android.Manifest.permission#READ_CONTACTS READ_CONTACTS}    .</p>
    373 
    374 <pre>
    375 public static class CursorLoaderListFragment extends ListFragment
    376     implements OnQueryTextListener, LoaderManager.LoaderCallbacks&lt;Cursor&gt; {
    377 
    378     // This is the Adapter being used to display the list's data.
    379   SimpleCursorAdapter mAdapter;
    380 
    381   // If non-null, this is the current filter the user has provided.
    382   String mCurFilter;
    383 
    384   @Override public void onActivityCreated(Bundle savedInstanceState) {
    385     super.onActivityCreated(savedInstanceState);
    386 
    387     // Give some text to display if there is no data. In a real
    388     // application this would come from a resource.
    389     setEmptyText(&quot;No phone numbers&quot;);
    390 
    391     // We have a menu item to show in action bar.
    392     setHasOptionsMenu(true);
    393 
    394     // Create an empty adapter we will use to display the loaded data.
    395     mAdapter = new SimpleCursorAdapter(getActivity(),
    396         android.R.layout.simple_list_item_2, null,
    397         new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
    398         new int[] { android.R.id.text1, android.R.id.text2 }, 0);
    399     setListAdapter(mAdapter);
    400 
    401     // Prepare the loader. Either re-connect with an existing one,
    402     // or start a new one.
    403     getLoaderManager().initLoader(0, null, this);
    404   }
    405 
    406   @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    407     // Place an action bar item for searching.
    408     MenuItem item = menu.add(&quot;Search&quot;);
    409     item.setIcon(android.R.drawable.ic_menu_search);
    410     item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    411     SearchView sv = new SearchView(getActivity());
    412     sv.setOnQueryTextListener(this);
    413     item.setActionView(sv);
    414   }
    415 
    416   public boolean onQueryTextChange(String newText) {
    417     // Called when the action bar search text has changed. Update
    418     // the search filter, and restart the loader to do a new query
    419     // with this filter.
    420     mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
    421     getLoaderManager().restartLoader(0, null, this);
    422     return true;
    423   }
    424 
    425   @Override public boolean onQueryTextSubmit(String query) {
    426     // Don't care about this.
    427     return true;
    428   }
    429 
    430   @Override public void onListItemClick(ListView l, View v, int position, long id) {
    431     // Insert desired behavior here.
    432     Log.i(&quot;FragmentComplexList&quot;, &quot;Item clicked: &quot; + id);
    433   }
    434 
    435   // These are the Contacts rows that we will retrieve.
    436   static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    437     Contacts._ID,
    438     Contacts.DISPLAY_NAME,
    439     Contacts.CONTACT_STATUS,
    440     Contacts.CONTACT_PRESENCE,
    441     Contacts.PHOTO_ID,
    442     Contacts.LOOKUP_KEY,
    443   };
    444   public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) {
    445     // This is called when a new Loader needs to be created. This
    446     // sample only has one Loader, so we don't care about the ID.
    447     // First, pick the base URI to use depending on whether we are
    448     // currently filtering.
    449     Uri baseUri;
    450     if (mCurFilter != null) {
    451       baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
    452           Uri.encode(mCurFilter));
    453     } else {
    454       baseUri = Contacts.CONTENT_URI;
    455     }
    456 
    457     // Now create and return a CursorLoader that will take care of
    458     // creating a Cursor for the data being displayed.
    459     String select = &quot;((&quot; + Contacts.DISPLAY_NAME + &quot; NOTNULL) AND (&quot;
    460         + Contacts.HAS_PHONE_NUMBER + &quot;=1) AND (&quot;
    461         + Contacts.DISPLAY_NAME + &quot; != '' ))&quot;;
    462     return new CursorLoader(getActivity(), baseUri,
    463         CONTACTS_SUMMARY_PROJECTION, select, null,
    464         Contacts.DISPLAY_NAME + &quot; COLLATE LOCALIZED ASC&quot;);
    465   }
    466 
    467   public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
    468     // Swap the new cursor in. (The framework will take care of closing the
    469     // old cursor once we return.)
    470     mAdapter.swapCursor(data);
    471   }
    472 
    473   public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
    474     // This is called when the last Cursor provided to onLoadFinished()
    475     // above is about to be closed. We need to make sure we are no
    476     // longer using it.
    477     mAdapter.swapCursor(null);
    478   }
    479 }</pre>
    480 <h3 id="more_examples"> </h3>
    481 
    482 <p>        <strong>ApiDemos</strong>
    483  .</p>
    484 <ul>
    485   <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderCursor.html">
    486 LoaderCursor</a>      
    487 .</li>
    488   <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html"> LoaderThrottle</a>      
    489           .</li>
    490 </ul>
    491 
    492 <p>SDK       <a href="http://developer.android.com/resources/samples/get.html">
    493 </a> . </p>
    494 
    495