Why is CursorAdapter different from BaseAdapter?

2019-05-07 06:53发布

I would like to ask why CursorAdapter splits the process of creating a view and populating it with data into newView() and bindView() while BaseAdapter only does this with getView() ?

2条回答
Deceive 欺骗
2楼-- · 2019-05-07 07:28

If you check CurosrAdapter source code you can see, that in getView method, both newView and bindView methods are used. Method newView is executed only when there is no view, thus it can spare the creation of some objects. Method bindView is always called and it purpouse is to update view data.

查看更多
叛逆
3楼-- · 2019-05-07 07:32

From Source code of CursorAdapter.java, CursorAdapter extends BaseAdapter.
And you can see getView() function implementation:

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;
    } 

Its do what we usually do in getView() (inflate the view if convertView is null, otherwise reuse the view), so its just for make it easier for the developer OR force the user to use ViewHolder pattern.

PS: Some devs calls bindViews() function in there newView() implementation, from the source code you can see there is no need for that.

查看更多
登录 后发表回答