Android API Guides---ListView

列表显示
ListView控件是一个显示可滚动的项目列表视图组。列表项使用,拉从源内容,诸如数组或数据库查询,并将其转换每个项目导致成被放置到列表视图中的一个转接器自动插入到列表中。

的介绍,你怎么能动态插入使用适配器的看法,阅读与适配器建筑平面布置

Android API Guides---ListView_第1张图片

使用装载机


使用CursorLoader是查询光标作为一个异步任务,以避免与查询阻止你的应用程序的主线程的标准方式。当CursorLoader收到光标结果,L​​oaderCallbacks收到回调onLoadFinished(),这是你更新了新光标和列表视图然后显示结果的适配器。


虽然CursorLoader API是在Android 3.0的(API级别11)首先介绍,他们也是在支持库可用,以便同时支持运行Android 1.6或更高版本的设备您的应用程序可以使用它们。


有关使用装载机异步加载数据的详细信息,请参阅装载机指南。





下面的示例使用ListActivity,它是包括一个ListView作为默认其唯一布局元件的活性。它执行一个查询到联系人提供程序的名字和电话号码的列表。


活动实现了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
    }
}
注:由于此示例执行的联系人提供的查询,如果你想尝试这个代码,你的应用程序必须要求在manifest文件中READ_CONTACTS权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />



你可能感兴趣的:(java,android,ListView,sdk)