CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁。

首先看一下CursorAdapter的部分源码:

  1. /**
  2. * @see android.widget.ListAdapter# getView(int, View, ViewGroup)
  3. */
  4. public View getView( int position, View convertView, ViewGroup parent) {
  5. if (!mDataValid) {
  6. throw new IllegalStateException( "this should only be called when the cursor is valid");
  7. }
  8. if (!mCursor.moveToPosition(position)) {
  9. throw new IllegalStateException( "couldn't move cursor to position " + position);
  10. }
  11. View v;
  12. if (convertView == null) {
  13. v = newView( mContext, mCursor, parent);
  14. } else {
  15. v = convertView;
  16. }
  17. bindView(v, mContext, mCursor);
  18. return v;
  19. }

可以看出CursorAdapter是继承了BaseAdapter后覆盖它的getView方法在getView方法中调用了newView和bindView方法,我们在写CursorAdapter时必须实现它的两个方法。

  1. public abstract View newView (Context context, Cursor cursor, ViewGroup parent);
  2.  
  3. public abstract void bindView(View view, Context context, Cursor cursor);
从源码可以看出:
  • newView ( ):并不是每次都被调用的,它只在实例化的时候调用,数据增加的时候也会调用,但是在重绘(比如修改条目里的TextView的内容)的时候不会被调用
  • bindView ( ):从代码中可以看出在绘制Item之前一定会调用bindView方法它在重绘的时候也同样被调用

继承CursorAdapter的示例部分代码:

  1. @Override
  2. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  3.  
  4. ViewHolder viewHolder= new ViewHolder();
  5. LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
  6. View view=inflater.inflate(R.layout.item_contacts ,parent,false);
  7.  
  8. viewHolder. tv_name=(TextView) view.findViewById(R.id.tv_showusername );
  9. viewHolder. tv_phonenumber=(TextView) view.findViewById(R.id.tv_showusernumber );
  10. view.setTag(viewHolder);
  11. Log. i("cursor" ,"newView=" +view);
  12. return view;
  13. }
  14.  
  15. @Override
  16. public void bindView(View view, Context context, Cursor cursor) {
  17. Log. i("cursor" ,"bindView=" +view);
  18. ViewHolder viewHolder=(ViewHolder) view.getTag();
  19. //从数据库中查询姓名字段
  20. String name=cursor.getString(cursor.getColumnIndex(PersonInfo.NAME));
  21. //从数据库中查询电话字段
  22. String phoneNumber=cursor.getString(cursor.getColumnIndex(PersonInfo.PHONENUMBER));
  23.  
  24. viewHolder. tv_name.setText(name);
  25. viewHolder. tv_phonenumber.setText(phoneNumber);
  26. }

缺点:

  1. 直接把Cursor暴露在UI层,写了很多 cursor.getString(cursor.getColumnIndex("scheme"))类似的代码.直接把Cursor暴露在ui层是很不好的示范,Cursor是非常底层的数据模型,不应该暴露在ui层。
  2. SQLite 加载超过1M数据速度会变得很慢.一旦数据量大了整个UI的速度立刻降了下来。
  3. RecyclerView 并不支持 CursorAdapter

总之,CursorAdapter这种方式并不是很好的设计典范。

Android CursorAdapter的使用的更多相关文章

  1. Android CursorAdapter

    CursorAdapter 继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁.            public abstract class     Cur ...

  2. Android CursorAdapter的使用详解

    一.CursorAdapter介绍 CursorAdapter这个类是继承于BaseAdapter的它是一个虚类它为Cursor和ListView连接提供了桥梁 二.CursorAdapter详解 1 ...

  3. Android 3.0 r1 API中文文档(108) —— ExpandableListAdapter

    前言 本章内容是android.widget.ExpandableListAdapter,版本为Android 3.0  r1,翻译来自"深夜未眠",欢迎访问它的博客:" ...

  4. 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter

    Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...

  5. Android开发——利用Cursor+CursorAdapter实现界面实时更新

    好久没有更新博客了.不是没时间写,而是太懒.而且感觉有些东西没有时间总结,之之后再想写,就想不起来了.晚上新发现一点东西,所以就及时写下来. 最近利用业余时间在看Android的Download模块, ...

  6. 【Android】13.2 使用自定义的CursorAdapter访问SQLite数据库

    分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 SQliteDemo1的例子演示了SimpleCursorAdapter的用法,本节我们将使用用途更广的自定义的游 ...

  7. Android如果动态改变CursorAdapter Item个数

    //adapter内部类 private class SearchAdapter extends CursorAdapter { @Override public View newView(Conte ...

  8. Android MVP模式 谷歌官方代码解读

    Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...

  9. Android软件开发之ListView 详解【转】

    ListView的使用方法  ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘 ...

随机推荐

  1. HDU4292 Food —— 最大流 + 拆点

    题目链接:https://vjudge.net/problem/HDU-4292 Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  2. html5--6-52 动画效果-过渡

    html5--6-52 动画效果-过渡 实例 @charset="UTF-8"; div{ width: 300px; height: 150px; margin: 30px; f ...

  3. python之路,day7-面向对象变成

    本篇内容: 面向对象.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 一.面向对象高级语法部分 静态方法: #@staticmethod只是名义上归类管理,实际上跟类没什么关系 ...

  4. J2ee的SSM和SSH的小结

    1.介绍SSM框架: SSM是指由Spring.SpringMVC.Mybatis三个开源框架整合的开发框架. a).Spring是一个轻量级的容器框架,核心是控制反转(IoC)和面向切面(AOP). ...

  5. Windows可以ping通百度,但是用浏览器打不开网页

    开始——>运行——>输入cmd回车——>输入: netsh winsock reset  命令(重置winsock文件)——>重启系统. 重启完系统,即可解决:如不能,请再查找 ...

  6. hibernate的基础学习--一对一关联

    一对一关系以丈夫和妻子模型 配置文件 妻子配置文件: <?xml version="1.0" encoding="utf-8" ?> <!DO ...

  7. 从开发的角度对zigbee安全的杂谈

    说起zigbee应该很少人听过,这个B名字怪怪的... 以前开发不懂开发的思想,前前后后花了很久时间,现在回想起来,突然想从安全的角度来理解数据的传输 废话:伴随科技的快速演进,物联网(The Int ...

  8. (水题)洛谷 - P1478 - 陶陶摘苹果(升级版)

    https://www.luogu.org/problemnew/show/P1478 没啥好说的…… 居然还漏写一个等于号WA了一发. #include<bits/stdc++.h> u ...

  9. 793. Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  10. 洛谷P2569 [SCOI2010]股票交易(单调队列)

    传送门 惭愧……这种题目都没看出来…… 首先,我们用$dp[i][j]$表示在第$i$天,手上有$j$股时的最大收益 第一,我们可以直接买股票,即$dp[i][j]=-j*AP_i$,这个直接计算即可 ...