androidUI第四部分---1.4.3ListView用法—利用CursorAdapter生成ListView
来源:互联网 发布:程序员职业规划 知乎 编辑:程序博客网 时间:2024/05/22 04:06
CursorAdapter是个抽象类,一般用他的直接子类simplecursorAdapter,废话少说上代码:
ListView lv = (ListView)this.findViewById(R.id.listview);
Cursor cur = this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
Log.v("1111", cur.getColumnCount()+"no");
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.listviewitem,cur,new String[]{Contacts.People.NAME},new int[]{R.id.text});
lv.setAdapter(sca);
由于CursorAdapter是关联数据库里的数据的,这里呢可以直接用联系人电话簿来做测试调用方法this.getContentResolver().query()可以获得一个cursor的对象,可以理解成一个查询的结果记录集,可以把这个例子和simplecursor的例子对比来看生成simplecursorAdapter对象时传的参数意义就很明显了,最后两个参数可以理解成将对应的记录集里的名子显示到listviewitem的id为text的控件上。
本例子需要在AndroidManifest.xml文件中添加permission不然没法读取电话簿内容,如下:
<uses-permission android:name= "android.permission.READ_CONTACTS"></uses-permission>
Listviewitem.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
>
</TextView>
</LinearLayout>
Main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation ="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width ="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
></ListView>
</RelativeLayout>
- androidUI第四部分---1.4.3ListView用法—利用CursorAdapter生成ListView
- androidUI第四部分--1.4.1ListView用法—利用ArrayAdapter生成ListView
- androidUI第四部分---1.4.2ListView用法—利用SimpleAdapter生成ListView
- androidUI ListView
- listview自定义CursorAdapter
- ListView 利用CursorAdapter 和 LoaderManager从数据库中获取数据
- AndroidUI组件 ListView(2)
- baseadapter和cursoradapter与ListView
- 【Android开发】ListView使用CursorAdapter
- listview上的CursorAdapter使用
- AndroidUI--SimpleAdapter CursorAdapter
- 通过CursorAdapter在ListView中的数据呈现
- 通过CursorAdapter在ListView中的数据呈现
- ListView通过CursorAdapter显示数据库内容
- ListView结合CursorAdapter自动更新view的流程
- ListView+CursorAdapter+LoaderManger的配合使用
- ListView通过CursorAdapter显示数据库内容
- ListView通过CursorAdapter显示数据库内容
- perl比较两个文件,输出不同行
- [DELPHI]TQuery的结果到入Excel
- 杭电1229 还是A+B
- MSSQL求連續ID內數量合計-常見解決方案
- zjut 1176 斐波那契数列
- androidUI第四部分---1.4.3ListView用法—利用CursorAdapter生成ListView
- 有感1
- 18个Joomla教程和模板网站
- TXT文本中到中文无法显示
- 袋鼠书的驯兽师作者教Erlang
- SQL Server2005 表分区三步曲
- 使用特征自定义序列化
- 剖析SQL Server执行计划
- 校园数字化建设--注册中心投标文件研究(16)--综合服务门户系统设计方案