Android CursorAdapter的使用
CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁。
首先看一下CursorAdapter的部分源码:
/**
* @see android.widget.ListAdapter# getView(int, View, ViewGroup)
*/
public View getView( int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException( "this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException( "couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView( mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
可以看出CursorAdapter是继承了BaseAdapter后覆盖它的getView方法在getView方法中调用了newView和bindView方法,我们在写CursorAdapter时必须实现它的两个方法。
public abstract View newView (Context context, Cursor cursor, ViewGroup parent); public abstract void bindView(View view, Context context, Cursor cursor);
- newView ( ):并不是每次都被调用的,它只在实例化的时候调用,数据增加的时候也会调用,但是在重绘(比如修改条目里的TextView的内容)的时候不会被调用
- bindView ( ):从代码中可以看出在绘制Item之前一定会调用bindView方法它在重绘的时候也同样被调用
继承CursorAdapter的示例部分代码:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder viewHolder= new ViewHolder();
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view=inflater.inflate(R.layout.item_contacts ,parent,false); viewHolder. tv_name=(TextView) view.findViewById(R.id.tv_showusername );
viewHolder. tv_phonenumber=(TextView) view.findViewById(R.id.tv_showusernumber );
view.setTag(viewHolder);
Log. i("cursor" ,"newView=" +view);
return view;
} @Override
public void bindView(View view, Context context, Cursor cursor) {
Log. i("cursor" ,"bindView=" +view);
ViewHolder viewHolder=(ViewHolder) view.getTag();
//从数据库中查询姓名字段
String name=cursor.getString(cursor.getColumnIndex(PersonInfo.NAME));
//从数据库中查询电话字段
String phoneNumber=cursor.getString(cursor.getColumnIndex(PersonInfo.PHONENUMBER)); viewHolder. tv_name.setText(name);
viewHolder. tv_phonenumber.setText(phoneNumber);
}
缺点:
- 直接把Cursor暴露在UI层,写了很多
cursor.getString(cursor.getColumnIndex("scheme"))类似的代码.直接把Cursor暴露在ui层是很不好的示范,Cursor是非常底层的数据模型,不应该暴露在ui层。 - SQLite 加载超过1M数据速度会变得很慢.一旦数据量大了整个UI的速度立刻降了下来。
- RecyclerView 并不支持 CursorAdapter
总之,CursorAdapter这种方式并不是很好的设计典范。
Android CursorAdapter的使用的更多相关文章
- Android CursorAdapter
CursorAdapter 继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁. public abstract class Cur ...
- Android CursorAdapter的使用详解
一.CursorAdapter介绍 CursorAdapter这个类是继承于BaseAdapter的它是一个虚类它为Cursor和ListView连接提供了桥梁 二.CursorAdapter详解 1 ...
- Android 3.0 r1 API中文文档(108) —— ExpandableListAdapter
前言 本章内容是android.widget.ExpandableListAdapter,版本为Android 3.0 r1,翻译来自"深夜未眠",欢迎访问它的博客:" ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
- Android开发——利用Cursor+CursorAdapter实现界面实时更新
好久没有更新博客了.不是没时间写,而是太懒.而且感觉有些东西没有时间总结,之之后再想写,就想不起来了.晚上新发现一点东西,所以就及时写下来. 最近利用业余时间在看Android的Download模块, ...
- 【Android】13.2 使用自定义的CursorAdapter访问SQLite数据库
分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 SQliteDemo1的例子演示了SimpleCursorAdapter的用法,本节我们将使用用途更广的自定义的游 ...
- Android如果动态改变CursorAdapter Item个数
//adapter内部类 private class SearchAdapter extends CursorAdapter { @Override public View newView(Conte ...
- Android MVP模式 谷歌官方代码解读
Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...
- Android软件开发之ListView 详解【转】
ListView的使用方法 ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘 ...
随机推荐
- stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
1.StringBuffer类的概述 * A:StringBuffer类概述 * 通过JDK提供的API,查看StringBuffer类的说明 * 1.线程安全的可变字符序列. * 2.可将字符串缓冲 ...
- Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索
Lucene 的四大索引查询 清单1:使用布尔操作符 Java代码 //Test boolean operator blic void testOperator(String indexD ...
- 【opencv】opencv在图片、视频嵌中英文字符的方法
转自:http://www.cnblogs.com/hujingshuang/p/5119015.html 说明:本博文是根据前人已有的成果并结合自己的理解而成的.为了避免让读者感到繁琐,我将运用小学 ...
- 黑客技术 —— Linux 命令行
很多和正则表达式 re 是一致的: 1. 修改上次执行的命令 删除多余部分: % grep fooo /var/log/auth.log % ^o % grep foo /var/log/auth.l ...
- 喵哈哈村的魔法考试 Round #1 (Div.2)
比赛地址:http://qscoj.cn/contest/2/ 都是中文题,这里不在详述题意 A.喵哈哈村的魔法石 分析:暴力求解 #include<iostream> #include& ...
- 打印斐波那契(Fibonacci)数列
需求:打印 Fibonacci数列 思路: 当前项的值等于前两项数值的和 F=(F-1)+F(F-2) 样例: 输入:10 输出:1 1 2 3 5 8 13 21 34 55 辗转相加法实现 #in ...
- css 样式引入的方法 link 与import的区别
<link> 元素所参考的样式用户可以自由的选择加以改变,而导入的样式表单就自动的与剩下的样式表融合在一起了 CSS与HTML文档结合的4中方法:1 使用<link>元素链接到 ...
- 树莓派Dietpi系统配置Dueros
dietpi 系统安装alsa工具 安装依赖 sudo apt-get install python-dateutil sudo apt-get install gir1.2-gstreamer- ...
- vim 退出命令(保存、放弃保存)
在命令模式中,连按两次大写字母Z,若当前编辑的文件曾被修改过,则Vi保存该文件后退出,返回到shell:若当前编辑的文件没被修改过,则Vi直接退出, 返回到shell. 在末行模式下,输入命令 : ...
- python __builtins__ complex类 (13)
13.'complex', 函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数.如果第一个参数为字符串,则不需要指定第二个参数. class complex(ob ...