使用CursorLoader異步加載數(shù)據(jù)
Android 3.0引入了CursorLoader實(shí)現(xiàn)異步加載數(shù)據(jù),為了避免同步查詢數(shù)據(jù)庫(kù)時(shí)阻塞UI線程的問(wèn)題。在API 11之前可以通過(guò)下載支持庫(kù),來(lái)使之前的系統(tǒng)支持此功能。
下面是一個(gè)例子:
public class ListViewLoader extends ListActivity
implements LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter;
// These are the Contacts rows that we will retrieve
static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME};
// This is the select criteria
static final String SELECTION = "((" +
ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
ContactsContract.Data.DISPLAY_NAME + " != '' ))";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a progress bar to display while the list loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar);
// Must add the progress bar to the root of the layout
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);
// For the cursor adapter, specify which columns go into which views
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1
// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
PROJECTION, SELECTION, null, null);
}
// Called when a previously created loader has finished loading
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);
}
// Called when a previously created loader is reset, making the data unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法
這篇文章主要介紹了Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法,涉及Android操作多媒體文件及系統(tǒng)硬件設(shè)備的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android AnalogClock簡(jiǎn)單使用方法實(shí)例
這篇文章主要介紹了Android AnalogClock簡(jiǎn)單使用方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了AnalogClock的布局調(diào)用技巧,需要的朋友可以參考下2016-01-01
怎么發(fā)布打包并發(fā)布自己的Android應(yīng)用(APP)
前面我為大家講的都是關(guān)于Android開發(fā)方面的知識(shí)點(diǎn)和技術(shù),不少朋友可能會(huì)感到疑惑--究竟我該怎么打包、發(fā)布自己開發(fā)的APP,怎樣將我的APP放到網(wǎng)上工別人下載,怎樣保證我的APP安全及版權(quán)問(wèn)題呢2013-11-11
Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音
這篇文章主要介紹了Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音,涉及Android音頻操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android ListView 條目多樣式展示實(shí)例詳解
這篇文章主要介紹了Android ListView 條目多樣式展示的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android編程中FileOutputStream與openFileOutput()的區(qū)別分析
這篇文章主要介紹了Android編程中FileOutputStream與openFileOutput()的區(qū)別,結(jié)合實(shí)例形式分析了FileOutputStream與openFileOutput()的功能,使用技巧與用法區(qū)別,需要的朋友可以參考下2016-02-02

