Android CursorAdapter的使用详解
一、CursorAdapter介绍
CursorAdapter这个类是继承于BaseAdapter的它是一个虚类它为Cursor和ListView连接提供了桥梁
二、CursorAdapter详解
1.CursorAdapter的继承关系图
从图中可以看出CursorAdapter是继承于BaseAdapter的,它有一个直接的子类SimpleCursorAdapter
2.CursorAdapter的用法
我们首先看一下CursorAdapter的部分源码:
/**
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
} else {
return ;
}
} /**
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem( int position) {
if (mDataValid && mCursor != null) {
mCursor.moveToPosition(position);
return mCursor;
} else {
return null;
}
} /**
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId( int position) {
if (mDataValid && mCursor != null) {
if ( mCursor.moveToPosition(position)) {
return mCursor.getLong( mRowIDColumn);
} else {
return ;
}
} else {
return ;
}
} /**
* @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时必须实现它的两个方法
/**
* Makes a new view to hold the data pointed to by cursor.
* @param context Interface to application's global information
* @param cursor The cursor from which to get the data. The cursor is already
* moved to the correct position.
* @param parent The parent to which the new view is attached to
* @return the newly created view.
*/
public abstract View newView (Context context, Cursor cursor, ViewGroup parent); /**
* Bind an existing view to the data pointed to by cursor
* @param view Existing view, returned earlier by newView
* @param context Interface to application's global information
* @param cursor The cursor from which to get the data. The cursor is already
* moved to the correct position.
*/
public abstract void bindView(View view, Context context, Cursor cursor);
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*
* @param cursor The new cursor to be used
*/
public void changeCursor (Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
swapCursor(cusor)的源码如下: /**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*
* @param newCursor The new cursor to be used.
* @return Returns the previously set Cursor, or null if there wasa not one.
* If the given new Cursor is the same instance is the previously set
* Cursor, null is also returned.
*/
public Cursor swapCursor (Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if ( mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver );
if ( mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver );
}
mCursor = newCursor;
if (newCursor != null) {
if ( mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver );
if ( mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver );
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id" );
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -;
mDataValid = false;
// notify the observers about the lack of a data set
notifyDataSetInvalidated();
}
return oldCursor;
}
之前我一直对cursor是怎么移动的疑惑,比方说cursor中有40条数据,那么它是怎样一行一行移动cursor把这40条数据显示出来的,看过源码后发现其实很简单,

在EditText中输入姓名和电话,点击保存后会显示在下面的listView中
@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);
}
(2)点击保存按钮执行的方法
private void setClickListener() { btn_save.setOnClickListener( new OnClickListener() { public void onClick(View v) { userName=et_name.getText().toString();
userPhoneNumber=et_phonenumber .getText().toString(); if( userName.equals( "")){
Toast. makeText(MainActivity.this, "用户名不能为空!",).show();
return;
}
if( userPhoneNumber.equals( "")){
Toast. makeText(MainActivity.this,"电话不能为空", ).show();
return;
} ContentValues contentValues= new ContentValues();
contentValues.put(PersonInfo. NAME, userName);
contentValues.put(PersonInfo.PHONENUMBER ,userPhoneNumber );
//把EditText中的文本插入数据库
dataBase.insert(PersonInfo. PERSON_INFO_TABLE, null,contentValues);
//根据 _id 降序插叙数据库保证最后插入的在最上面
Cursor myCursor = dataBase.query(PersonInfo. PERSON_INFO_TABLE, null, null, null, null, null, orderBy);
//Cursor改变调用chanageCursor()方法
myCursorAdapter.changeCursor(myCursor);
}
});
}
Android CursorAdapter的使用详解的更多相关文章
- 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING
<Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th 2014 Email:skyseraph00@163.com 更多精彩请直接 ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- ANDROID L——Material Design详解(UI控件)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...
- android bundle存放数据详解
转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...
- Cordova 打包 Android release app 过程详解
Cordova 打包 Android release app 过程详解 时间 -- :: SegmentFault 原文 https://segmentfault.com/a/119000000517 ...
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- 给 Android 开发者的 RxJava 详解
我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Flipboard 的 Android 项目也在使用 RxJava ,并且使用的场景越来越多 .而最近这几个 ...
- Android中mesure过程详解
我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...
- Android中Intent组件详解
Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...
随机推荐
- vim-录制命令的使用
使用vim时无意间触碰到q键,左下角出现“recording”这个标识,这是vim的一个强大功能. 他可以录 制一个宏(Macro),在开始记录后,会记录你所有的键盘输入,包括在insert模式下的输 ...
- Button- 自定义控件添加自定义属性
今天自定义了一个button按钮,需要添加一个属性,具体步骤如下 1.新属性的信息设定:在values目录下添加attrs.xml文件,在里面添加属性信息 <?xml version=" ...
- Funui-overlay 如何添加theme 的 overlay
昨天更改theme主题的时候,发现所有仓库下的theme都是共用的.也就是说,如果你更改了52平台下的theme,那么你提交了代码以后,82下也会发生相应的更改.但是,昨天修改的theme属性,只在3 ...
- vue.js有什么用,是用来做什么的(整理)
vue.js有什么用,是用来做什么的(整理) 一.总结 一句话总结:用数据绑定的思想,vue可以简单写单个页面,也可以写一个大的前端系统,也可以做手机app的界面. 1.Vue.js是什么? 渐进式框 ...
- angularjs 合并单元格
Html: <table class="table table-striped"> <thead> <tr> <th>国家</ ...
- WPF MVVM示例自定义模板数据绑定
在触摸屏设备上.由于列表是的信息展示不是非常直观和便捷操作. 所以也就出现了很多用面板控件:类似win10的Metro风格, 所以抽空做了一个WPF面板控件. 话不多上 , 先上一个示例图. 为了便于 ...
- PHP: php_ldap.dll不能加载解决方案
PHP: php_ldap.dll不能加载解决方案 php.ini中开启 ldap的扩展后,重启服务:phpinfo();中没有ldap apache_error.log 提示:PHP Warning ...
- 洛谷——U10223 Cx大帝远征埃及
https://www.luogu.org/problem/show?pid=U10223#sub 题目背景 众所周知,Cx是一个宇宙大犇.Cx能文善武,一直在为大一统的实现而努力奋斗着.Cx将调用他 ...
- [D3] Reuse Transitions in D3 v4
D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This ...
- noi25 最长最短单词(为什么会出现运行时错误)
noi25 最长最短单词(为什么会出现运行时错误) 一.总结 一句话总结:比如除以零,数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出 1.c++报runtime er ...