List View
2015-08-04 22:12
232 查看
List View
注:这里的翻译指的是对文章关键点做的说明和评注,而非逐字翻译。ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.
For an introduction to how you can dynamically insert views using an adapter, read Building Layouts with an Adapter.
Using a Loader
Using a CursorLoader is the standard way to query a Cursor as an asynchronous task in order to avoid blocking your app’s main thread with the query. When the CursorLoader receives the Cursor result, the LoaderCallbacks receives a callback to onLoadFinished(), which is where you update your Adapter with the new Cursor and the list view then displays the results.Although the CursorLoader APIs were first introduced in Android 3.0 (API level 11), they are also available in the Support Library so that your app may use them while supporting devices running Android 1.6 or higher.
For more information about using a Loader to asynchronously load data, see the Loaders guide.
Example
The following example uses ListActivity, which is an activity that includes a ListView as its only layout element by default. It performs a query to the Contacts Provider for a list of names and phone numbers.The activity implements the LoaderCallbacks interface in order to use a CursorLoader that dynamically loads the data for the list view.
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 } }
Note: Because this sample performs a query on the Contacts Provider, if you want to try this code, your app must request the READ_CONTACTS permission in the manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
相关文章推荐
- 高效 MacBook 工作环境配置
- Docker源码分析(四):Docker Daemon之NewDaemon实现
- iOS中的字符串操作大全
- C 结构体的用法总结
- angularJS--apply() 、digest()和watch()方法
- angularJS--apply() 、digest()和watch()方法
- mybatis 源码系列 组件之 reflection
- pl/sql develop Command Windows 命令
- android Looper Handler机制
- POJ - 3660 - Cow Contest (floyd求传递闭包)
- hdu 5349 MZL's simple problem
- Cubieboard 3编译驱动教程
- C语言:链表的创建、增、删、查
- C#编程:读二进制文件的优化
- N-Queens
- 解决ScrollView 中的listView只显示一行的问题:
- 怎样在不打开Xcode的情况下打开模拟器
- HDU1.2.7 Higher Math
- AOP系列(二)——为符合条件的目标对象创建代理
- Java集合小结