CursorAdapter

继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁。           
public abstract class
    CursorAdapter
        extends BaseAdapter
直接子类只有ResourceCursorAdapter
Class Overview
Adapter that exposes data from a Cursor to a ListView widget. 
The Cursor must include a column named "_id" or this class will not work.
注意cursor的必须要有个命名为"_id"的列。比如Contacts._ID就为"_id"
必须实现以下函数
abstract View newView(Context  context, Cursor  cursor, ViewGroup  parent)
    Makes a new view to hold the data pointed to by cursor.
abstract void  bindView(View  view, Context  context, Cursor  cursor)
    Bind an existing view to the data pointed to by cursor
注意
newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。
【总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view】
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.TextView;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloCursor extends ListActivity {
private static String[] PROJECTION = new String[] { Contacts._ID,
Contacts.DISPLAY_NAME }; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,
null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");
startManagingCursor(c);
MyCursorAdapter adapter = new MyCursorAdapter(this, R.layout.list_row,
c);
this.setListAdapter(adapter);
Button button = (Button)findViewById(R.id.Button01);
OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View v) {
doAction();
}
};
button.setOnClickListener(listener);
mHandler = new Handler(); } private String[] mStrings = { "hubin", "hudashi", "robin" };
int cnt = ;
private Handler mHandler; class AddContactThread implements Runnable {
public void run() {
int nStringLength = mStrings.length;
int randomNumber = ;
ContentValues newValues = new ContentValues();
String tempString = null;
randomNumber = (int) (Math.random() % );
for (int i = ; i < nStringLength; i++) {
tempString = mStrings + cnt + randomNumber;
newValues.put(Contacts.DISPLAY_NAME, tempString);
getContentResolver().insert(RawContacts.CONTENT_URI, newValues);
newValues.clear(); }
cnt++;
}
}
AddContactThread addContact=new AddContactThread();
void doAction()
{
mHandler.post(addContact);
}
}
class MyCursorAdapter extends CursorAdapter {
Context context=null;
int viewResId;
public MyCursorAdapter(Context context, int resource, Cursor cursor) {
super(context,cursor);
viewResId=resource;
}
public View newView(Context context, Cursor cursor, ViewGroup parent) { TextView view =null;
LayoutInflater vi = null;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =(TextView)vi.inflate(viewResId, parent, false);
//v =(TextView)vi.inflate(textViewResourceId,null);
Log.i("hubin","newView"+view);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.i("hubin","bind"+view);
TextView nameView = (TextView) view;
// Set the name
nameView.setText(cursor
.getString(cursor.getColumnIndex("DISPLAY_NAME")));
}
}
一、结构

public abstract class CusrorAdapter extends BaseAdpater implements Filterable

java.lang.Object

android.widget.BaseAdapter

         android.widget.CursorAdapter

直接子类

      ResourceCursorAdapter

间接子类

SimpleCursorAdapter

  二、概述

    通过该类可以用Cursor的方式访问数据库,并将查询出来的数据展示到列表视图(ListView)部件上。其中游标携带的结果集中必须有列名为“_id”的列,否则这个类无法工作。

  三、构造函数

    public CusorAdapter(Context context,Cursor c)   

           构造函数。每当数据库的数据发生改变时,适配器将调用requery()重新查询以显示最新的数据。

             参数

       context        应用程序上下文。

  c                 用来获取数据的游标(Coursor)

    public CusorAdapter(Context context,Cursor c, boolean autoRequery)       

  构造函数。每当数据库的数据发生改变时,适配器将调用requery()重新查询以显示最新的数据。

             参数

       context        应用程序上下文。

  c                 用来获取数据的Coursor

                      autoRequry     设置为true时,每当数据库的数据发生改变时,适配器将调用requery()重新查询以显示最新的数据。

  四、公共方法

  public abstract void bindView (View view, Context context, Cursor cursor)

  重用一个已有的view,使其显示当前cursor所指向的数据。

  参数

                   view                已存在的视图, 返回之前newView方法创建的视图。

                   context          应用程序上下文

                   cursor               用于获取数据的Coursor。Coursor已经移到正确的位置。

  public void changeCursor (Cursor cursor)

           更改底层的游标为新传入的游标。如果游标已经存在则先关闭这个已存在的游标。

  参数                  

                   cursor               新Cursor。

  public CharSequence convertToString (Cursor cursor)

           将cursor转换成CharSequence。子类应该重写这个方法并转换它们的结果。这个方法的默认实现是:当cursor为空时返回一个空串,否则直接返回调用cursor的toString()方法。

  参数        

                   cursor               将cursor转换成CharSequence对象。

           返回值

             返回表示CharSequence的值。

  public int getCount ()

         (译者注:获取适配器中数据的总行数。)

             参见

                      getCount()

  public Cursor getCursor ()

           返回当前适配器绑定的Cursor对象。

  返回值

            Cursor对象。

  public View getDropDownView (int position, View convertView, ViewGroup parent)

  获取下拉列表选项指定位置的视图对象

  参数

           position  视图(View)对象的行索引。

           convertView       重用已有的视图(View)对象。备注:在使用前你应该检查一下这个视图对象是否非空并且这个对象的类型是否合适。由此引伸出,如果该对象不能被转换并显示正确的数据,这个方法内部就会重新创建一个合适的视图(View)对象。

           parent  不管是转换后的还是重新创建的视图(View)对象,始终都会依附于parent对象上。

  返回值    

                   返回视图(View)对象,该对象显示数据集指定位置上的数据。

  public Filter getFilter ()

  返回一个可以通过一种过滤模式来约束数据的过滤器。

  这个方法通常在Adapter类实现。

  返回值

                   一个用于约束数据的过滤器

  public FilterQueryProvider getFilterQueryProvider ()

  返回一个提供过滤的查询过滤器。若过滤器为null,则不再过滤。

  返回值

                   返回当前过滤器对象,如果不存在返回null。

                 参见

                   setFilterQueryProvider(android.widget.FilterQueryProvider)

                   runQueryOnBackgroundThread(CharSequence)

  public Object getItem (int position)

  (译者注:获取数据集中指定位置上的数据项目)

                  参见

                       getItem(int)

  public long getItemId (int position)

         (译者注:获取数据集中的指定位置上的行id。)

             参见

                       getItemId(int)

  public View getView (int position, View convertView, ViewGroup parent)

  (译者注:获取一个显示数据集中指定位置数据段视图。可以手动创建视图,或者从XML设计文件填充。当视图从XML设计文件填充时,父视图(如GridView,ListView等)将接受默认的设计参数,除非使用inflate(int, android.view.ViewGroup, boolean)去指定一个根视图和防止依附于根视图。)

        参见

                   getView(int, View, ViewGroup)

  public boolean hasStableIds ()

无论项ID代表的基础数据的是否变化都保持不变。

      返回值

  如果为TRUE,意味着相同的ID始终引用相同的对象。

  public View newDropDownView (Context context, Cursor cursor, ViewGroup parent)

  生成一个新的下拉视图来保存cursor指向的数据

           参数

             context  应用程序全局信息接口(应用上下文)

  cursor  获取数据的游标,它已经移动到正确的位置

  parent 与新视图相关联的上级视图

  返回值

  新创建的视图。

  public abstract View newView (Context context, Cursor cursor, ViewGroup parent)

  新建一个视图来保存cursor指向的数据

           参数

             context  应用程序全局信息接口(应用上下文)

  cursor  获取数据的游标,它已经移动到正确的位置

  parent 与新视图相关联的上级视图

  返回值

                   新创建的视图。

  public Cursor runQueryOnBackgroundThread (CharSequence constraint)

  执行含指定约束的查询。此查询依赖于适配器的过滤器。查询是由FilterQueryProvider提供。如果没有指定FilterQueryProvider,当前cursor不过滤只返回。该方法会通过changeCursor(Cursor)方法返回一个Cursor对象,并且关闭掉先前的Cursor对象。这个方法始终在后台线程执行,而不是在应用程序的主线程(或是UI线程)中运行。规定:当参数constraint为null或为空时,该方法返回原始结果。

        参数

              constraint 该查询必须被过滤的约束。

  返回值

  返回含有新的查询结果的Cursor。

  参考

         getFilter()

getFilterQueryProvider()

               setFilterQueryProvider(android.widget.FilterQueryProvider)

  public void setFilterQueryProvider (FilterQueryProvider filterQueryProvider)

  设置一个过滤器,用来过滤当前的Cursor对象。当这个适配器需要进行过滤操作时, runQuery(CharSequence)方法被调用。

       参数

  filterQueryProvider  过滤器对象,设置为null时,就相当于移除了该过滤器。

                参考

                       getFilterQueryProvider()

runQueryOnBackgroundThread(CharSequence)

  五、受保护方法

  protected void init (Context context, Cursor c, boolean autoRequery)

  (译者注:供构造函数使用初始化相关参数)

  protected void onContentChanged ()
     当cursor对象上的ContentObserver接收到改变的通知时就会调用该方法,其默认实现提供了自动重新查询方式,但可以被子类重写。

Android CursorAdapter的更多相关文章

  1. Android CursorAdapter的使用

    CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...

  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. Mac系统升级到10.9(mavericks)时安装php扩展问题解决(转)

    问题一: 执行执行 phpize 报错: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include ...

  2. Windows系统结构

    四种用户模式进程:1.系统支持进程,比如登录进程和会话管理器,并不是Windows服务,不有服务控制管理器启动2.服务进程,一些以Windows服务方式来运行的组件3.用户应用进程4.环境子系统服务器 ...

  3. JAVA并发实现四(守护线程和线程阻塞)

    守护线程     Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台 ...

  4. HBase面试问题

    一.HBase的特点是什么 1.HBase一个分布式的基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于数据结构字段 ...

  5. (转)iOS7界面设计规范(3) - UI基础 - 启动与退出

    周二晚间来第三发,搞得好像今天是周六的赶脚.发掉之后再奖励自己一点冰啤酒吧,然后扑床去.天气热起来了,各位注意防暑降温呗.走起. 重要:这是针对于正在开发中的API或技术的预备文档(预发布版本).虽然 ...

  6. 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)

    概述: 假设我们开发一个图片处理软件,里面肯定包括很多相关功能,比如说剪切,旋转,滤镜,美化等等,而我们这些功能所要处理的对象是固定的,就是我们所显示的那张图片.但是我们不能把所有的功能罗列到一个ta ...

  7. 为MyEclipse加入自己定义凝视

    非常多时候我们默认的MyEclipse的类凝视是这种,例如以下图 能够通过改动MyEclipse的凝视规则来改变,不但能够改动类的.还能够改动字段.方法等凝视规则,操作方法例如以下 1.针对方法的凝视 ...

  8. Activity中的startActivityResult,setResult,finish,onActivityResult的关系

    一:首先图示: 二:代码: 1:方法selectName public void selectName(View view){ Intent intent = new Intent(this,Name ...

  9. linux core dump学习

    1. core dump是什么? core dump又叫核心转储,当操作系统收到特定的signal时, 会生成某个进程的core dump文件.这样程序员可以根据 已经生成的core dump文件来d ...

  10. [RxJS] Updating Data with Scan

    You often need to update the data flowing through the stream with custom logic based on what you nee ...