Why is the CursorAdapter different from the BaseAdapter?

I would like to ask why the CursorAdapter splits the process of creating a view and populates it with data in newView() and bindView() , and BaseAdapter does it only with getView() ?

+4
source share
2 answers

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

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

It does what we usually do in getView() (inflate the view if convertView is null, otherwise the view is reused), so it’s easy to make it easier for the developer OR to force the user to use the ViewHolder template.

PS: Some developers call the bindViews () function in the implementation of newView (), from the source code you can see that this is not necessary.

+4
source

If you marked the source code with the CurosrAdapter , you can see that the getView method uses the newView and bindView . The newView method newView executed only when there is no view, so it can save on the creation of some objects. The bindView method bindView always called, and purpouse must update the view data.

+2
source

Source: https://habr.com/ru/post/1500047/


All Articles