列表視圖
阿新 • • 發佈:2017-06-24
retrieve exist 顯示 tar 接收 hat nload prev 由於
ListView
是一個顯示一列可滾動項目的視圖組。 系統使用 Adapter
自動將列表項目插入列表,適配器從來源(例如數組或數據庫查詢)提取內容,並將每個項目結果轉換為視圖放置到列表中。
有關如何使用適配器動態插入視圖的介紹,請閱讀使用適配器構建布局。
使用加載器
使用 CursorLoader
是以異步任務形式查詢 Cursor
的標準方式,可避免查詢阻塞應用的主線程。 當 CursorLoader
接收到 Cursor
結果時,LoaderCallbacks
會收到對 onLoadFinished()
的回調,從中您可以使用新的 Cursor
更新 Adapter
,然後列表視圖會顯示結果。
雖然 CursorLoader
API 首先是在 Android 3.0(API 級別 11)中引入,但支持庫中也有提供,因此您的應用可在使用這些 API 的同時,仍為 Android 1.6 或更高版本的設備提供支持。
如需了解有關使用 Loader
異步加載數據的詳細信息,請參閱加載器指南。
示例
下例使用 ListActivity
,該 Activity 包含一個 ListView
作為其僅有的默認布局元素。 它對聯系人提供程序執行查詢,以獲取姓名和電話號碼清單。
Activity 實現 LoaderCallbacks
接口,以使用 CursorLoader
為列表視圖動態加載數據。
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 } }
註:由於此示例在聯系人提供程序中執行查詢,因此,如果您想要試用此代碼,您的應用必須在清單文件中請求 READ_CONTACTS
權限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
列表視圖