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

来源:互联网 发布:淘宝退货退款钱退哪里 编辑:程序博客网 时间:2024/05/16 15:57

ListView是一个ViewGroup,用来显示滑动项的列表。使用一个适配器把列表项被自动的增加到列表。从一个资源如队列或数据库中查询内容,获取每个项的结果,放到在列表中显示的视图项中。

为了介绍如何使你能够动态的使用一个适配器加入视图,读Building Layouts with an Adapter。

http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews




一 使用一个加载器

使用一个CursorLoader,它是一个标准方式去查询一个Cursor,就像一个同步进程为了避免死锁你的应用程序的主线程。当这个CursorLoader接收到Cursor结果,这个LoaderCallbacks接收一个回调去onLoadFinished(),在这里你使用这个新的Cursor更新你的Adapter和列表视图,当显示结果的时候。


虽然CursorLoaderAIP在安卓3.0里第一介绍,他们同样被包含在Suport Library中,所有你可以在你的APP中使用他们以支持运行在安卓1.6以上的设备上。


二 实例

下面的实例中使用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    }}









原创粉丝点击