Android布局---List View
来源:互联网 发布:淘宝入驻知名品牌 编辑:程序博客网 时间:2024/05/16 18:02
本文译自:http://developer.android.com/guide/topics/ui/layout/listview.html
ListView是一个用于显示可滚动的项目列表的View组。它适应一个Adapter来自动的插入列表项,该Adapter会从诸如数组或数据库查询中拉入内容,并把每个项目结果转换成一个可放入列表的View。
对于如何使用adapter来自动的插入View的介绍,请看使用Adapter来创建布局。
使用装载器
为了避免查询时阻塞你的应用程序的主线程,使用CursorLoader异步任务来查询Cursor是一种标准的方法。当CursorLoader接收到Cursor结果时,LoaderCallbacks会收到一个对onLoadFinished()方法的回调,这是你用新的Cursor来更新你的Adapter的地方,然后列表View显示结果。
尽管CursorLoader API在Android3.0(API Level 11)中被首次引入,但是这些API在支持类库中也是有效的,因此在支持运行Android1.6以上的设备中,你的应用程序也可以使用。
有关使用Loader来异步加载数据的信息,请看Loaders指南。
示例
下面的示例使用了ListActivity,默认情况下,它是一个只包含一个ListView布局元素的Activity。它执行对Contacts Provider查询,来获得姓名和电话号码列表。
为了使用CursorLoader来给列表View自动的加载数据,这个Activity实现了LoaderCallbacks接口。
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" />
- Android布局---List View
- Android之View布局
- android View控件布局
- android常用view布局
- Android View 布局 过程
- Android -- List View
- android View的布局过程
- android view的相关布局
- Android GUI之View布局
- 百分比布局,android自定义View。
- Android 动态添加View布局
- android 自定义View 梯形布局
- android 重叠view 重叠布局
- Android 开发例子(image view list view)
- android list view分割线
- Android列表视图(List View)
- Android列表视图(List View)
- android UI布局 layer-list
- Shell脚本根据设备名自动获取设备号
- 怎样是一个double类型变量保存为n位小数
- 从一有序数组中,找到一出现次数最多的数
- 程序员 2013 新年计划
- Ruby的特点
- Android布局---List View
- Struts2的struts2.xml配置文件
- Word文档去除烦人的防复制宏保防功能(word文档宏保护破解技巧)
- 有一种女孩子,她们天性敏感。
- 黑马程序员__JAVA高新技术--反射、注解总结
- 1月17日Facebook正式在美国启动VOIP服务1月20日切断与通信应用Voxer联系
- linux kernel map 2.6
- char 转wchar_t 及wchar_t转char
- 如何开始学习计算机编程