Why is CursorAdapter different from BaseAdapter?

天涯浪子 提交于 2019-12-07 11:12:37

问题


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() ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18561744/why-is-cursoradapter-different-from-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!