[转载]Android应用空间之列表视图(ListView)介绍-Android开发-最全的Android开发资料-eoe移动开发者社区.
http://docs.eoeandroid.com/guide/topics/ui/layout/listview.html
作者:smilysas
更新时间:2012年7月28日
列表视图是一个成列显示滚动项的视图组合。成列的项都通过Adapter]被自动插入到列表中,其中,Adapter适配器可以从数组或者数据库查询中提取出内容并转化成列表视图中的项。
%E6%96%87%E4%BB%B6:Listview.png
* 使用一个加载者*
使用一个CursorLoader是避免一个异步任务查询光标Cursor时阻塞程序主线程的标准途径。当CursorLoader收到一个Cursor结果,LoaderCallbacks会收到一个对onLoadFinished()的回调,这时可以利用新的Cursor和列表视图更新Adapter并显示结果。
虽然CursorLoader函数在Android3.0(API级别 11)中才第一次引入,程序可以通过引入Support Library使用它们来支持运行Android 1.6及以上的设备。
要查看更多关于利用Loader异步加载数据的信息,请参看Loaders
* 例子:*
下面的例子是把ListView作为唯一默认布局元素的活动ListActivity。它完成向Contacts Provider查询姓名和电话号码清单的功能。
为了使用CursorLoader向列表动态加载数据,这个活动实现了LoaderCallbacks接口。
public class ListViewLoader extends ListActivity implements LoaderManager.LoaderCallbacks { // 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权限: