1 page.title=Trnh ti 2 parent.title=Hot ng 3 parent.link=activities.html 4 @jd:body 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>Trong ti liu ny</h2> 8 <ol> 9 <li><a href="#summary">Tng quan v API Trnh ti</a></li> 10 <li><a href="#app">S dng cc Trnh ti trong mt ng dng</a> 11 <ol> 12 <li><a href="#requirements"></a></li> 13 <li><a href="#starting">Khi ng mt Trnh ti</a></li> 14 <li><a href="#restarting">Khi ng li mt Trnh ti</a></li> 15 <li><a href="#callback">S dng cc Phng php Gi li LoaderManager</a></li> 16 </ol> 17 </li> 18 <li><a href="#example">V d</a> 19 <ol> 20 <li><a href="#more_examples">Thm V d</a></li> 21 </ol> 22 </li> 23 </ol> 24 25 <h2>Lp kha</h2> 26 <ol> 27 <li>{@link android.app.LoaderManager}</li> 28 <li>{@link android.content.Loader}</li> 29 30 </ol> 31 32 <h2>Cc mu lin quan</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>c gii thiu trong Android 3.0, trnh ti gip vic ti d liu khng ng b 43 trong mt hot ng hoc phn on tr nn d dng. Trnh ti c nhng c im sau:</p> 44 <ul> 45 <li>Chng sn c cho mi {@link android.app.Activity} v {@link 46 android.app.Fragment}.</li> 47 <li>Chng cung cp kh nng ti d liu khng ng b.</li> 48 <li>Chng theo di ngun d liu ca mnh v chuyn giao kt qu mi khi ni dung 49 thay i.</li> 50 <li>Chng t ng kt ni li vi con chy ca trnh ti cui cng khi c 51 to li sau khi cu hnh thay i. V th, chng khng cn truy vn li d liu 52 ca mnh.</li> 53 </ul> 54 55 <h2 id="summary">Tng quan v API Trnh ti</h2> 56 57 <p>C nhiu lp v giao din c th c lin quan trong khi s dng 58 cc trnh ti trong mt ng dng. Chng c tm tt trong bng ny.</p> 59 60 <table> 61 <tr> 62 <th>Lp/Giao din</th> 63 <th>M t</th> 64 </tr> 65 <tr> 66 <td>{@link android.app.LoaderManager}</td> 67 <td>Mt lp tm tt c lin kt vi {@link android.app.Activity} hoc 68 {@link android.app.Fragment} qun l mt hoc nhiu thc th {@link 69 android.content.Loader}.N gip ng dng qun l 70 cc thao tc chy lu hn cng vi vng i {@link android.app.Activity} 71 hoc {@link android.app.Fragment}; cng dng ph bin nht ca lp ny l khi dng vi 72 {@link android.content.CursorLoader}, tuy nhin, cc ng dng c t do ghi 73 trnh ti ca chnh mnh ti cc kiu d liu khc. 74 <br /> 75 <br /> 76 Ch c mt {@link android.app.LoaderManager} trn mi hot ng hoc phn on. Nhng mt {@link android.app.LoaderManager} c th c 77 nhiu trnh ti.</td> 78 </tr> 79 <tr> 80 <td>{@link android.app.LoaderManager.LoaderCallbacks}</td> 81 <td>Mt giao din gi li mt my khch tng tc vi {@link 82 android.app.LoaderManager}. V d, bn s dng phng php gi li {@link 83 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} 84 to mt trnh ti mi.</td> 85 </tr> 86 <tr> 87 <td>{@link android.content.Loader}</td> 88 <td>Mt lp tm tt c vai tr thc hin vic ti d liu khng ng b. y l 89 lp c bn cho mt trnh ti. Thng thng, bn s s dng {@link 90 android.content.CursorLoader}, nhng bn c th trin khai lp con ca chnh mnh. Trong khi 91 cc trnh ti ang hot ng, chng s theo di ngun d liu ca mnh v chuyn giao 92 kt qu mi khi ni dung thay i. </td> 93 </tr> 94 <tr> 95 <td>{@link android.content.AsyncTaskLoader}</td> 96 <td>Trnh ti tm tt c chc nng cung cp {@link android.os.AsyncTask} thc hin cng vic.</td> 97 </tr> 98 <tr> 99 <td>{@link android.content.CursorLoader}</td> 100 <td>Mt lp con ca {@link android.content.AsyncTaskLoader} c chc nng truy vn 101 {@link android.content.ContentResolver} v tr v mt {@link 102 android.database.Cursor}. Lp ny trin khai giao thc {@link 103 android.content.Loader} theo mt cch chun ha truy vn cc con chy, 104 xy dng trn {@link android.content.AsyncTaskLoader} thc hin truy vn con chy 105 trn mt lung nn sao cho n khng chn UI ca ng dng. S dng 106 trnh ti ny l cch tt nht ti d liu khng ng b t mt {@link 107 android.content.ContentProvider}, thay v phi thc hin mt truy vn c qun l thng qua 108 phn on hoc cc API ca hot ng.</td> 109 </tr> 110 </table> 111 112 <p>Cc lp v giao din trong bng trn l nhng thnh phn thit yu 113 m bn s s dng trin khai mt trnh ti trong ng dng ca mnh. Bn s khng cn tt c chng 114 cho tng trnh ti m bn to lp, nhng bn s lun cn mt tham chiu ti {@link 115 android.app.LoaderManager} khi to mt trnh ti v trin khai 116 mt lp {@link android.content.Loader} chng hn nh {@link 117 android.content.CursorLoader}. Cc phn sau y trnh by vi bn cch s dng nhng 118 lp v giao din ny trong mt ng dng.</p> 119 120 <h2 id ="app">S dng cc Trnh ti trong mt ng dng</h2> 121 <p>Phn ny m t cch s dng cc trnh ti trong mt ng dng Android. Mt 122 ng dng s dng trnh ti thng bao gm:</p> 123 <ul> 124 <li>Mt {@link android.app.Activity} hoc {@link android.app.Fragment}.</li> 125 <li>Mt thc th ca {@link android.app.LoaderManager}.</li> 126 <li>Mt {@link android.content.CursorLoader} ti d liu c d phng bi mt {@link 127 android.content.ContentProvider}. Hoc cch khc, bn c th trin khai lp con 128 ca {@link android.content.Loader} hoc {@link android.content.AsyncTaskLoader} ca chnh mnh ti 129 d liu t mt s ngun khc.</li> 130 <li>Mt trin khai cho {@link android.app.LoaderManager.LoaderCallbacks}. 131 y l ni bn to trnh ti mi v qun l cc tham chiu ca mnh ti cc 132 trnh ti hin c.</li> 133 <li>Mt cch hin th d liu ca trnh ti, chng hn nh {@link 134 android.widget.SimpleCursorAdapter}.</li> 135 <li>Mt ngun d liu, chng hn nh mt {@link android.content.ContentProvider}, khi s dng mt 136 {@link android.content.CursorLoader}.</li> 137 </ul> 138 <h3 id="starting">Khi ng mt Trnh ti</h3> 139 140 <p>{@link android.app.LoaderManager} qun l mt hoc nhiu thc th {@link 141 android.content.Loader} trong mt {@link android.app.Activity} hoc 142 {@link android.app.Fragment}. Ch c mt {@link 143 android.app.LoaderManager} trn mi hot ng hoc phn on.</p> 144 145 <p>Thng thng, bn 146 s khi to mt {@link android.content.Loader} bn trong phng php {@link 147 android.app.Activity#onCreate onCreate()} ca hot ng, hoc trong phng php 148 {@link android.app.Fragment#onActivityCreated onActivityCreated()} ca phn on. Bn 149 lm iu ny nh sau:</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>Phng php {@link android.app.LoaderManager#initLoader initLoader()} s ly nhng 156 tham s sau:</p> 157 <ul> 158 <li>Mt ID duy nht xc nh trnh ti. Trong v d ny, ID l 0.</li> 159 <li>Cc tham i ty chn cung cp cho trnh ti khi 160 xy dng (<code>null</code> trong v d ny).</li> 161 162 <li>Trin khai {@link android.app.LoaderManager.LoaderCallbacks}, phng php m 163 {@link android.app.LoaderManager} gi bo co cc s kin trnh ti. Trong v d ny 164 , lp cuc b trin khai giao din {@link 165 android.app.LoaderManager.LoaderCallbacks}, v th n chuyn mt tham chiu 166 ti chnh n, {@code this}.</li> 167 </ul> 168 <p>Lnh gi {@link android.app.LoaderManager#initLoader initLoader()} m bo rng mt trnh ti 169 c khi to v hin hot. N c hai kt qu c th xy ra:</p> 170 <ul> 171 <li>Nu trnh ti c quy nh bi ID tn ti, trnh ti c to lp cui cng 172 s c s dng li.</li> 173 <li>Nu trnh ti c quy nh bi ID <em>khng</em> tn ti, 174 {@link android.app.LoaderManager#initLoader initLoader()} s kch khi phng php 175 {@link android.app.LoaderManager.LoaderCallbacks}{@link android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()}. 176 y l ni bn trin khai m khi to v tr v mt trnh ti mi. 177 bn thm, hy xem phn <a href="#onCreateLoader">onCreateLoader</a>.</li> 178 </ul> 179 <p>D trong trng hp no, trin khai {@link android.app.LoaderManager.LoaderCallbacks} 180 cho c lin kt vi trnh ti, v s c gi khi 181 trng thi ca trnh ti thay i. Nu ti im thc hin lnh gi ny, hm gi ang trong trng thi 182 c khi ng ca n, v trnh ti c yu cu tn ti v khi to 183 d liu ca n, khi h thng s gi {@link 184 android.app.LoaderManager.LoaderCallbacks#onLoadFinished onLoadFinished()} 185 ngay lp tc (trong khi {@link android.app.LoaderManager#initLoader initLoader()}), 186 v th bn phi sn sng khi iu ny xy ra. Xem <a href="#onLoadFinished"> 187 onLoadFinished</a> tho lun thm v lnh gi li ny</p> 188 189 <p>Lu rng phng php {@link android.app.LoaderManager#initLoader initLoader()} 190 s tr v {@link android.content.Loader} c to lp, nhng bn khng 191 cn bt li mt tham chiu ti n. {@link android.app.LoaderManager} t ng qun l 192 vng i ca trnh ti. {@link android.app.LoaderManager} 193 khi ng v dng ti khi cn v duy tr trng thi ca trnh ti 194 v ni dung i km ca n. Nh hm , bn him khi tng tc trc tip vi cc trnh ti 195 (thng qua mt v d v vic s dng cc phng php trnh ti tinh chnh hnh vi 196 ca mt trnh ti, hy xem v d <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html"> LoaderThrottle</a>). 197 Bn thng s dng nht l cc phng php {@link 198 android.app.LoaderManager.LoaderCallbacks} can thip vo tin trnh ti 199 khi din ra mt s kin c bit. tho lun thm v ch ny, hy xem phn <a href="#callback">S dng Phng php Gi li LoaderManager</a>.</p> 200 201 <h3 id="restarting">Khi ng li mt Trnh ti</h3> 202 203 <p>Khi bn s dng {@link android.app.LoaderManager#initLoader initLoader()}, nh 204 trnh by bn trn, n s dng mt trnh ti hin hu vi ID c quy nh nu c. 205 Nu khng c, n s to mt trnh ti. Nhng i khi bn mun b d liu c ca mnh 206 v bt u li.</p> 207 208 <p> b d liu c ca mnh, hy s dng {@link 209 android.app.LoaderManager#restartLoader restartLoader()}. V d, vic 210 trin khai {@link android.widget.SearchView.OnQueryTextListener} ny s khi ng li 211 trnh ti khi truy vn ca ngi dng thay i. Trnh ti cn c khi ng li sao cho 212 n c th s dng b lc tm kim c iu chnh thc hin mt truy vn mi:</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">S dng cc Phng php Gi li LoaderManager</h3> 225 226 <p>{@link android.app.LoaderManager.LoaderCallbacks} l mt giao din gi li 227 cho php mt my khch tng tc vi {@link android.app.LoaderManager}. </p> 228 <p>Cc trnh ti, c bit l {@link android.content.CursorLoader}, c k vng s 229 gi li d liu ca chng sau khi b dng. iu ny cho php ng dng gi li 230 d liu ca chng qua hot ng hoc cc phng php {@link android.app.Activity#onStop 231 onStop()} v {@link android.app.Activity#onStart onStart()} ca phn on, sao cho khi 232 ngi dng quay li mt ng dng, h khng phi ch d liu 233 ti li. Bn s dng cc phng php {@link android.app.LoaderManager.LoaderCallbacks} 234 khi cn bit khi no th nn to mt trnh ti mi, v thng bo vi ng dng khi no 235 th n lc dng s dng d liu ca mt trnh ti.</p> 236 237 <p>{@link android.app.LoaderManager.LoaderCallbacks} bao gm nhng phng php 238 sau:</p> 239 <ul> 240 <li>{@link android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} 241 Khi to v tr v mt {@link android.content.Loader} mi cho ID cho. 242 </li></ul> 243 <ul> 244 <li> {@link android.app.LoaderManager.LoaderCallbacks#onLoadFinished onLoadFinished()} 245 c gi khi mt trnh ti c to trc hon tt vic ti. 246 </li></ul> 247 <ul> 248 <li>{@link android.app.LoaderManager.LoaderCallbacks#onLoaderReset onLoaderReset()} 249 c gi khi mt trnh ti c to trc ang c t li, v th m khin d liu 250 ca n khng sn c. 251 </li> 252 </ul> 253 <p>Nhng phng php ny c m t chi tit hn trong cc phn sau.</p> 254 255 <h4 id ="onCreateLoader">onCreateLoader</h4> 256 257 <p>Khi bn nh truy cp mt trnh ti (v d, thng qua {@link 258 android.app.LoaderManager#initLoader initLoader()}), n kim tra xem 259 trnh ti c quy nh bi ID c tn ti khng. Nu khng, n s kch khi phng php {@link 260 android.app.LoaderManager.LoaderCallbacks} {@link 261 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()}. y 262 l lc bn to mt trnh ti mi. Thng thng s c mt {@link 263 android.content.CursorLoader}, nhng bn c th trin khai lp con {@link 264 android.content.Loader} ca chnh mnh. </p> 265 266 <p>Trong v d ny, phng php gi li {@link 267 android.app.LoaderManager.LoaderCallbacks#onCreateLoader onCreateLoader()} 268 s to mt {@link android.content.CursorLoader}. Bn phi xy dng 269 {@link android.content.CursorLoader} bng cch s dng phng php hm dng ca n m yu cu 270 trn b thng tin cn thit thc hin mt truy vn ti {@link 271 android.content.ContentProvider}. C th, n cn:</p> 272 <ul> 273 <li><em>uri</em> URI ca ni dung cn truy xut. </li> 274 <li><em>d tho</em> Mt danh sch cc ct s tr v. Vic chuyn 275 <code>null</code> s tr v tt c ct, iu ny khng hiu qu. </li> 276 <li><em>la chn</em> Mt b lc khai bo cc hng no s tr v, 277 c nh dng nh mt mnh SQL WHERE (khng gm chnh mnh WHERE). Vic chuyn 278 <code>null</code> s tr v tt c hng cho URI cho. </li> 279 <li><em>selectionArgs</em> Bn c th thm ?s vo la chn, 280 chng s c thay th bng cc gi tr t <em>selectionArgs</em>, theo th t xut hin trong 281 la chn. Gi tr s c gn kt thnh cc Xu. </li> 282 <li><em>sortOrder</em> Cch sp xp th t cc hng, c nh dng nh mt mnh SQL 283 ORDER BY (khng bao gm chnh mnh ORDER BY). Vic chuyn <code>null</code> s 284 s dng th t sp xp mc nh, iu ny c th dn n kt qu khng theo th t.</li> 285 </ul> 286 <p>V d:</p> 287 <pre> 288 // If non-null, this is the current filter the user has provided. 289 String mCurFilter; 290 ... 291 public Loader<Cursor> 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 = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" 307 + Contacts.HAS_PHONE_NUMBER + "=1) AND (" 308 + Contacts.DISPLAY_NAME + " != '' ))"; 309 return new CursorLoader(getActivity(), baseUri, 310 CONTACTS_SUMMARY_PROJECTION, select, null, 311 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 312 }</pre> 313 <h4 id="onLoadFinished">onLoadFinished</h4> 314 315 <p>Phng php ny c gi khi mt trnh ti c to trc hon thnh vic ti ca mnh. 316 Phng php ny c bo m s c gi trc khi gii phng d liu cui cng 317 c cung cp cho trnh ti ny. Ti im ny, bn nn loi b mi trng hp s dng 318 d liu c (do n s c gii phng sm), nhng khng nn 319 t mnh gii phng d liu do trnh ti s hu d liu v s m nhn vic ny.</p> 320 321 322 <p>Trnh ti s gii phng d liu sau khi n bit ng dng ang khng cn 323 s dng n na. V d, nu d liu l mt con chy t mt {@link 324 android.content.CursorLoader}, bn khng nn t mnh gi {@link 325 android.database.Cursor#close close()} trn d liu . Nu con chy ang c t 326 trong mt {@link android.widget.CursorAdapter}, bn nn s dng phng php {@link 327 android.widget.SimpleCursorAdapter#swapCursor swapCursor()} sao cho 328 {@link android.database.Cursor} c khng b ng. V d:</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<Cursor> 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>Phng php ny c gi khi mt trnh ti c to trc ang c t li, v th m khin 344 d liu ca n khng sn c. Lnh gi li ny cho php bn tm hiu xem khi no th d liu 345 s c gii phng bn c th loi b tham chiu ca mnh ti n. </p> 346 <p>S trin khai ny gi ra 347 {@link android.widget.SimpleCursorAdapter#swapCursor swapCursor()} 348 vi mt gi tr <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<Cursor> 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">V d</h2> 364 365 <p>Ly mt v d, sau y l trin khai y ca {@link 366 android.app.Fragment} c chc nng hin th mt {@link android.widget.ListView} cha 367 kt qu ca mt truy vn i vi trnh cung cp ni dung danh b. N s dng mt {@link 368 android.content.CursorLoader} qun l truy vn trn trnh cung cp.</p> 369 370 <p> mt ng dng truy cp danh b ca mt ngi dng, nh minh ha trong v d ny, bn k khai 371 ca n phi bao gm quyn 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<Cursor> { 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("No phone numbers"); 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("Search"); 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("FragmentComplexList", "Item clicked: " + 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<Cursor> 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 = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" 460 + Contacts.HAS_PHONE_NUMBER + "=1) AND (" 461 + Contacts.DISPLAY_NAME + " != '' ))"; 462 return new CursorLoader(getActivity(), baseUri, 463 CONTACTS_SUMMARY_PROJECTION, select, null, 464 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 465 } 466 467 public void onLoadFinished(Loader<Cursor> 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<Cursor> 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">Thm V d</h3> 481 482 <p>C mt vi mu khc trong <strong>ApiDemos</strong> 483 minh ha cch s dng cc trnh ti:</p> 484 <ul> 485 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderCursor.html"> 486 LoaderCursor</a> Mt phin bn hon chnh ca 487 on m HTML trnh by trn.</li> 488 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.html"> LoaderThrottle</a> Mt v d v cch s dng iu chnh gim 489 s truy vn m mt trnh cung cp ni dung thc hin khi d liu ca n thay i.</li> 490 </ul> 491 492 <p> bit thng tin v vic ti xung v ci t cc mu SDK, hy xem phn <a href="http://developer.android.com/resources/samples/get.html"> Ti 493 Mu</a>. </p> 494 495