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组件 绘 ...
随机推荐
- MYSQL进阶学习笔记四:MySQL存储过程之定义条件,处理过程及存储过程的管理!(视频序号:进阶_11,12)
知识点五:MySQL存储过程之定义条件和处理过程及存储过程的管理(11,12) 定义条件和处理: 条件的定义和处理可以用来定义在处理过程中遇到的问题时相应的处理步骤. DECLARE CONTINUE ...
- 让 SyntaxHighlighter 3.x 支持 Lua 语法着色
1. [代码]shBrushLua.js /** * SyntaxHighlighter * http://alexgorbatchev.com/SyntaxHighlighter * * Synta ...
- SPOJ:Help BTW(二分)
BTW wants to buy a gift for her BF and plans to buy an integer array. Generally Integer arrays are c ...
- Luogu网校听课笔记(自用
TG助学课——noilinux 8.2 深夜 Noi Linux的使用——darkflames的博客 #include<bits/stdc++.h> using namespace std ...
- 后台接口平台 基于Laravel 开发 快速开发数据接口
laravelPCMS V1.5.0 项目地址:https://github.com/q1082121/laravelcms 喜欢的朋友可以支持下 点点星标 百牛信息技术bainiu.ltd整理发布于 ...
- Struts2 文件上传 之 文件类型 allowedTypes
转自:https://www.cnblogs.com/zxwBj/p/8546889.html '.a' : 'application/octet-stream', '.ai' : ...
- 洛谷 - P2887 - 防晒霜Sunscreen - 贪心
https://www.luogu.org/problemnew/show/P2887 感觉可以: 把防晒霜拆点限制流量为瓶数,奶牛拆点限制流量为1,当某个防晒霜与奶牛匹配时连一条边,求最大流.但是这 ...
- 退出ACM?
我不知道为什么我有这样的想法,纵观CCCC,太弱太弱,再不把自己埋起来,狠起来,就真的非常菜了,去刷难题吧!我不管老郭的数据,只管自己的实力,每天三道难题CF的C题+总结.以及刷水题!!!(刷CF的B ...
- bzoj 5495: [2019省队联测]异或粽子【可持久化trie+大根堆】
和bzoj4504差不多,就是换了个数据结构 像超级钢琴一样把五元组放进大根堆,每次取一个出来拆开,(d,l,r,p,v)表示右端点为d,左端点区间为(l,r),最大区间和值为v左端点在p上 关于怎么 ...
- 解决 Xshell 连接出现 The remote SSH server rejected X11 forwarding request 问题
问题描述 使用 Xshell 5 首次连接虚拟机 CentOS 7.6 出现这样的提示: WARNING! The remote SSH server rejected X11 forwarding ...