Android onLoadFinished与onLoaderReset
onLoadFinished
這個(gè)方法是在前面已創(chuàng)建的加載器已經(jīng)完成其加載過程后被調(diào)用,這個(gè)方法保證會(huì)在應(yīng)用到加載器上的數(shù)據(jù)被釋放之前被調(diào)用。在此方法中,你必須刪除所有對(duì)舊數(shù)據(jù)的使用(因?yàn)樗鼘⒑芸鞎?huì)被刪除),但是不要自己去釋放它們,因?yàn)樗鼈兊募虞d器會(huì)做這些事情。
加載器一旦了解到應(yīng)用不再使用數(shù)據(jù)時(shí),將馬上釋放這些數(shù)據(jù)。例如,如果數(shù)據(jù)是一個(gè)從CursorLoader來的游標(biāo),你不應(yīng)調(diào)用游標(biāo)的close(),如果游標(biāo)被放置在一個(gè)CursorAdapter中,你應(yīng)使用swapCursor()方法,以使舊的游標(biāo)不被關(guān)閉。例如:
//這個(gè)Adapter被用于顯示列表的數(shù)據(jù). SimpleCursorAdapter mAdapter; ... public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Swap the new cursor in. (The framework will take care of closing the // old cursor once we return.) mAdapter.swapCursor(data); }onLoaderReset
當(dāng)一個(gè)已創(chuàng)建的加載器被重置從而使其數(shù)據(jù)無效時(shí),此方法被調(diào)用。此回調(diào)使你能發(fā)現(xiàn)什么時(shí)候數(shù)據(jù)將被釋放于是你可以釋放對(duì)它的引用。
下面這個(gè)實(shí)現(xiàn)調(diào)用參數(shù)為null的swapCursor():
// 這個(gè)Adapter被用于顯示列表的數(shù)據(jù) SimpleCursorAdapter mAdapter; ... public void onLoaderReset(Loader<Cursor> loader) { //此處是用于上面的onLoadFinished()的游標(biāo)將被關(guān)閉時(shí)執(zhí)行,我們需確保我們不再使用它mAdapter.swapCursor(null); }例子
作為一個(gè)例子,這里完整實(shí)現(xiàn)了一個(gè)Fragment顯示一個(gè)包含從聯(lián)系人contentprovider 返回查詢數(shù)據(jù)的ListView的內(nèi)容的功能,它使用一個(gè)CursorLoader來管理對(duì)provider的查詢。
public static class CursorLoaderListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> { // 這是用于顯示列表數(shù)據(jù)的Adapter SimpleCursorAdapter mAdapter; // 如果非null,這是當(dāng)前的搜索過慮器 String mCurFilter; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // 如果列表中沒有數(shù)據(jù),就給控件一些文字去顯示.在一個(gè)真正的應(yīng)用 // 中這應(yīng)用資源中取得. setEmptyText("No phone numbers"); // 我們?cè)趧?dòng)作欄中有一個(gè)菜單項(xiàng). setHasOptionsMenu(true); // 創(chuàng)建一個(gè)空的adapter,我們將用它顯示加載后的數(shù)據(jù) mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, new int[] { android.R.id.text1, android.R.id.text2 }, 0); setListAdapter(mAdapter); // 準(zhǔn)備loader.可能是重連到一個(gè)已存在的或開始一個(gè)新的 getLoaderManager().initLoader(0, null, this); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // 放置一個(gè)動(dòng)作欄項(xiàng)用于搜索. MenuItem item = menu.add("Search"); item.setIcon(android.R.drawable.ic_menu_search); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); SearchView sv = new SearchView(getActivity()); sv.setOnQueryTextListener(this); item.setActionView(sv); } public boolean onQueryTextChange(String newText) { // 在動(dòng)作欄上的搜索字串改變時(shí)被調(diào)用.更新 //搜索過濾器,并重啟loader來執(zhí)行一個(gè)新的查詢 mCurFilter = !TextUtils.isEmpty(newText) ? newText : null; getLoaderManager().restartLoader(0, null, this); return true; } @Override public boolean onQueryTextSubmit(String query) { // 我們不關(guān)心這個(gè)方法 return true; } @Override public void onListItemClick(ListView l, View v, int position, long id) { // 寫入你想寫的代碼 Log.i("FragmentComplexList", "Item clicked: " + id); } // 這是我們想獲取的聯(lián)系人中一行的數(shù)據(jù). static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS, Contacts.CONTACT_PRESENCE, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, }; public Loader<Cursor> onCreateLoader(int id, Bundle args) { // 當(dāng)一個(gè)新的loader需被創(chuàng)建時(shí)調(diào)用.本例僅有一個(gè)Loader, //所以我們不需關(guān)心ID.首先設(shè)置base URI,URI指向的是聯(lián)系人 Uri baseUri; if (mCurFilter != null) { baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); } else { baseUri = Contacts.CONTENT_URI; } // 現(xiàn)在創(chuàng)建并返回一個(gè)CursorLoader,它將負(fù)責(zé)創(chuàng)建一個(gè) // Cursor用于顯示數(shù)據(jù) String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); } public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // 將新的cursor換進(jìn)來.(框架將在我們返回時(shí)關(guān)心一下舊cursor的關(guān)閉) mAdapter.swapCursor(data); } public void onLoaderReset(Loader<Cursor> loader) { //在最后一個(gè)Cursor準(zhǔn)備進(jìn)入上面的onLoadFinished()之前. // Cursor要被關(guān)閉了,我們需要確保不再使用它. mAdapter.swapCursor(null); } }?
轉(zhuǎn)載于:https://www.cnblogs.com/zhujiabin/p/6400226.html
總結(jié)
以上是生活随笔為你收集整理的Android onLoadFinished与onLoaderReset的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一阶段冲刺 第二天
- 下一篇: python进程、线程、协程