Android列表视图(List View)
Android列表视图(ListView)
ListView是一个显示滚动项列表的示视图组(viewgroup),通过使用适配器(Adapter)把这些列表项自动插入到列表中。适配器比如从一个数组或是数据库查询获取到数据,然后转换每一项成为可放入到列表的视图。
列表的显示需要三个元素:
(1) ListView:用来展示列表的view。
(2) 适配器:用来把数据映射到ListView上。
(3) 数据:具体的将被映射的字符串、图片或是基本组件。
图1
对于我们如何使用适配器来动态插入view,参考链接:
http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
1. 使用加载器(Using a Loader)
为了实现异步加载数据,且为了避免在查询时阻塞APP主线程,从Android3.0开始引入CursorLoader,这是一种查询Cursor的标准方式。当CursorLoader接收到Cursor结果(查询结束),LoaderCallbacks接收到对onLoadFinished()方法的回调,此方法使用新的Cursor来更新适配器,然后列表视图显示这个结果。
虽然CursorLoader的API是从Android3.0(API 11)开始引入的,但可以通过使用支持的库来使用:http://developer.android.com/tools/extras/support-library.html,但要求Android系统版本在1.6及之后的版本。
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 whilethe 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 ofthe layout
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);
// For the cursor adapter, specify whichcolumns go into which views
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
int[] toViews = {android.R.id.text1}; // The TextViewin simple_list_item_1
// Create an empty adapter we will use todisplay the loaded data.
// We pass null for the cursor, then updateit in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);
// Prepare the loader. Eitherre-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 CursorLoaderthat will take care of
// creating a Cursor for the data beingdisplayed.
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. (Theframework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}
// Called when a previously created loader is reset, making the dataunavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursorprovided to onLoadFinished()
// above is about to be closed. We needto 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
}
}
链接:
Android开发者:
http://developer.android.com/guide/topics/ui/layout/listview.html
Android之ListView原理学习与优化总结
http://mzh3344258.blog.51cto.com/1823534/889879
androidListView详解
http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
Android的CursorLoader用法小结
http://www.linuxidc.com/Linux/2013-05/84572.htm
Android列表视图(List View)相关推荐
- android表视图,android – 列表视图与表格布局
我正在开发一个活动,其中我使用列表视图与表格布局. 我想在列表视图中显示图像和文本,但问题是图像显示略微超出列表顺序 img1 text1 img1 text1 img2 text2 and i wa ...
- Android列表视图(ListView--SimpleAdapter)学习
接上文 上一篇文章使用的是ListView中最简单的ArrayAdapter,而且可以很容易的看出,ArrayAdapter比较适合 单一的显示一些文本信息.不过这并不表明使用ArrayAdapter ...
- 列表视图——List View
一,List View认识 1.什么是List View? 显示一个可滚动的单列列表. 2.如何实现List View: 1.在布局文件中声明一个ListView并且是这个控件编写的独有的id信息. ...
- Android 列表视图
第一步 value/strings.xml <?xml version="1.0" encoding="utf-8"?> <resources ...
- Android 列表视图制作古诗词
文档目录 一.案例操作 1.创建安卓应用[ReadAncientPoetry] 2.将背景图片拷贝到drawable目录 3.布局资源文件activity_main.xml 4.诗歌列表项模板poem ...
- android可扩展列表,android-可扩展列表视图未显示
代码可以正常运行,没有任何错误或警告,但可扩展列表未显示,这是代码 public class MainActivity extends Activity implements ExpandableLi ...
- android 文本分页显示,Android查看带有列表视图的分页器并动态添加文本
我在一个分片活动中有一个Viewpager,它有一个带有编辑文本和发送按钮的botton框架. 在片段布局中,我有一个ListView,并在片段中附加了一个适配器.现在我正在实现从片段中的Parent ...
- android 更新适配器,android – 当适配器数据更改时更新列表视图
当与阵列适配器关联的数据更改时,使列表视图无效足以显示更新的值?下面的代码不工作,我误解了这里的东西. public class ZeroItemListActivity extends Activi ...
- 实验七 Android Studio Intent相机图库读取照片【班级名单列表视图03】
一.实验目的 掌握Intent启动外部应用并交互数据的方法,了解Android权限和访问外部文件的方法. 二.实验内容 在SQLite版本的班级名单基础上实现以下功能 1. 设计修改列表项的Fragm ...
最新文章
- RenderTextureFormat.ShadowMap
- Puppet 实验三 安装和配置
- Java知多少(4)J2SE、J2EE、J2ME的区别
- pyflink的local模式实验记录
- mysql5.5索引如何定义_MySQL5.5索引数在InnoDB引擎内与索引数在mysql中定义的数量是不一致问题-阿里云开发者社区...
- 15寸计算机显示器,苹果确认15寸RMBP支持全球首款5K显示器
- APP自动化测试-Appium日志分析
- windows 用户管理
- python保存图片到指定路径_[Python03] 5分钟学会3种方法给模块添加路径!
- Python 项目打包
- linux操作系统实验目的,Linux操作系统实验报告.doc
- qnap安装Linux程序,播酷云J3455-itx 安装 黑威联通Qnap 4.4.0(实机安装)
- Windows7包含哪些版本
- 华为路由设置虚拟服务器命令,华为路由器配置ip命令
- 锦鱼课堂:跪着赚快钱,一年20W你愿意吗?
- 树莓派摄像头——图像 视频采集
- html3d建模,数百个 HTML5 例子学习 HT 图形组件 – 3D 建模篇
- 路由器管理页面html,路由器的管理登陆页面【入口地址】
- nrcellcu和nrcelldu_华为5G常用MML命令(现场版本)
- 使用git拉取远程仓库代码
热门文章
- python 作用域 前缀_Python 之作用域和名字空间
- python绘制函数怎么去掉原点_python – 更改绘图的原点
- 服务器 操作系统安装到sd卡,DELL服务器通过sd卡安装系统(iDRAC Use vFlash ).doc
- windowsserver2008r2可以装什么模拟器_为什么不可以用塑料桶装汽油?答案就在这!...
- .net core 文件流保存图片_使用JSDelivr加速Github、博客文件
- 新概念一册电子书课本_新概念英语第二册完整版:音频+动画视频+课本图文讲解...
- mysql象限和投影_Camera类之orthographic-摄像机投影模式(第100篇随笔)
- java k线绘制,用Java绘制K线图[Java编程]
- html复选框对齐文字,input【type=checkbox】标签与字体对齐
- xlst 解析 html c,怎樣實現利用xslt把xml文件內容顯示到html文件中?急!