1. 程式人生 > >安卓---API GUIDES---List View

安卓---API GUIDES---List View

ListView是一個ViewGroup,用來顯示滑動項的列表。使用一個介面卡把列表項被自動的增加到列表。從一個資源如佇列或資料庫中查詢內容,獲取每個項的結果,放到在列表中顯示的檢視項中。

為了介紹如何使你能夠動態的使用一個介面卡加入檢視,讀Building Layouts with an Adapter。


一 使用一個載入器

使用一個CursorLoader,它是一個標準方式去查詢一個Cursor,就像一個同步程序為了避免死鎖你的應用程式的主執行緒。當這個CursorLoader接收到Cursor結果,這個LoaderCallbacks接收一個回撥去onLoadFinished(),在這裡你使用這個新的Cursor更新你的Adapter和列表檢視,當顯示結果的時候。

雖然CursorLoaderAIP在安卓3.0裡第一介紹,他們同樣被包含在Suport Library中,所有你可以在你的APP中使用他們以支援執行在安卓1.6以上的裝置上。

二 例項

下面的例項中使用ListActivity,這個活動包含一個ListView。它執行一個查詢到Contacts Provider獲取一個名字和電話號碼的列表。

這個活動實現這個LoaderCallbacks介面,為了使用一個CursorLoader,它動態載入資料到列表檢視。

publicclassListViewLoaderextendsListActivityimplementsLoaderManager
.LoaderCallbacks<Cursor>{// This is the Adapter being used to display the list's dataSimpleCursorAdapter mAdapter;// These are the Contacts rows that we will retrievestaticfinalString[] PROJECTION =newString[]{ContactsContract.Data._ID,ContactsContract.Data.DISPLAY_NAME};// This is the select criteria
staticfinalString SELECTION ="(("+ContactsContract.Data.DISPLAY_NAME +" NOTNULL) AND ("+ContactsContract.Data.DISPLAY_NAME +" != '' ))";@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// Create a progress bar to display while the list loadsProgressBar progressBar =newProgressBar(this);         progressBar.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.CENTER));         progressBar.setIndeterminate(true);         getListView().setEmptyView(progressBar);// Must add the progress bar to the root of the layoutViewGroup root =(ViewGroup) findViewById(android.R.id.content);         root.addView(progressBar);// For the cursor adapter, specify which columns go into which viewsString[] 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 =newSimpleCursorAdapter(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 createdpublicLoader<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.returnnewCursorLoader(this,ContactsContract.Data.CONTENT_URI,                 PROJECTION, SELECTION,null,null);}// Called when a previously created loader has finished loadingpublicvoid 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 unavailablepublicvoid 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);}@Overridepublicvoid onListItemClick(ListView l,View v,int position,long id){// Do something when a list item is clicked}}