1. 程式人生 > >Android官方文件—User Interface(Layouts)(List View)

Android官方文件—User Interface(Layouts)(List View)

List View

ListView是一個顯示可滾動項列表的檢視組。列表項使用介面卡自動插入到列表中,該介面卡從諸如陣列或資料庫查詢之類的源中提取內容,並將每個專案結果轉換為放入列表中的檢視。

有關如何使用介面卡動態插入檢視的介紹,請閱讀使用介面卡構建佈局。

使用裝載程式


使用CursorLoader是將Cursor作為非同步任務進行查詢的標準方法,以避免使用查詢阻止應用程式的主執行緒。當CursorLoader接收到Cursor結果時,LoaderCallbacks會收到onLoadFinished()的回撥,這是用新Cursor更新Adapter並在列表檢視中顯示結果的地方。

儘管CursorLoader API最初是在Android 3.0(API級別11)中引入的,但它們也可以在支援庫中使用,以便您的應用可以在支援執行Android 1.6或更高版本的裝置時使用它們。

有關使用Loader非同步載入資料的更多資訊,請參閱Loaders指南。

示例


以下示例使用ListActivity,它是一個預設包含ListView作為其唯一佈局元素的活動。它會向Contacts Provider執行查詢,以獲取名稱和電話號碼列表。

該活動實現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
    }
}

注意:由於此示例在Contacts Provider上執行查詢,因此如果要嘗試此程式碼,則應用程式必須在清單檔案中請求READ_CONTACTS許可權:<uses-permission android:name =“android.permission.READ_CONTACTS”/>