什么场合适合用CurcorAdapter?

什么场合适合用CurcorAdapter?
ListView要读取数据表中的数据。
1.CursorAdapter的继承关系图
CursorAdapter继承于BaseAdapter,有一个直接子类SimpleCursorAdapter。
技术分享

2.CursorAdapter的用法

CursorAdapter继承了BaseAdapter后覆盖它的getView方法,在getView方法中调用了newView和bindView方法,我们在写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;
}

从源码的 getView( int position, View convertView, ViewGroup parent)方法中我们可以看出:
(1)newView:并不是每次都被调用的,它只在实例化的时候调用,数据增加的时候也会调用,但是在重绘(比如修改条目里的TextView的内容)的时候不会被调用;
(2)bindView:从代码中可以看出在绘制Item之前一定会调用bindView方法它在重绘的时候也同样被调用。

3. CursorAdapter还有一个重要的方法   public   void  changeCursor (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();
    }
}
/**
 * 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 = -1;
        mDataValid = false;
        // notify the observers about the lack of a data set
        notifyDataSetInvalidated();
    }
    return oldCursor;
}
调用此方法后会把当前的mCursor设置为新传过来的cursor,同时把原来的cursor返回并关闭。
作用:当我们的Cursor变化时调用此方法
adapter.changeCursor(cursor),它的功能类似于 adapter.notifyDataSetChanged()方法
4.Cursor的移动

cursor是怎么移动的?比方说cursor中有40条数据, 那么它是怎样一行一行移动cursor把这40条数据显示出来的?
getCount()方法中 return   mCursor .getCount();然后在getView方法的时候调用了 mCursor .moveToPosition(position)其实和BaseAdapter的原理是一样的,这样就可以一条一条的绘制条目了。

参考网址:http://www.bubuko.com/infodetail-734550.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值