GetView Vs. BindView in a custom CursorAdapter?

JavaAndroidUser InterfaceAdapter

Java Problem Overview


So, I'm watching this video http://www.youtube.com/watch?v=N6YdwzAvwOA and Romain Guy is showing how to make more efficient UI adapter code using the getView() method. Does this apply to CursorAdapters as well? I'm currently using bindView() and newView() for my custom cursor adapters. Should I be using getView instead?

Java Solutions


Solution 1 - Java

CursorAdapter has an implementation of getView() that delegates to newView() and bindView(), in such a way as enforces the row recycling pattern. Hence, you do not need to do anything special with a CursorAdapter for row recycling if you are overriding newView() and bindView().

Solution 2 - Java

/**
     * @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;
    }

This CursorAdapter source code, clearly cursorAdapter work more.

Solution 3 - Java

The CursorAdapter implementation is different from sub-classing regular adapters like BaseAdapter, you don't need to override getView(), getCount(), getItemId() because that information can be retrieved from the cursor itself.

Given a Cursor, you only need to override two methods to create a CursorAdapter subclass:

bindView() : Given a view, update it to display the data in the provided cursor.

newView() : This gets called to consctruct a new view that goes into the the list.

The CursorAdapter will take care of recycling views (unlike the getView() method on regular Adapter). It doesn't call the newView() each time it needs a new row. If it already has a View(not null), it will directly call the bindView(), this way, the created view is reused. By splitting the creation and population of each view into these two methods, the CursorAdapter achieves view reuse where as, in regular adapters, both these things are done in getView() method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopher PerryView Question on Stackoverflow
Solution 1 - JavaCommonsWareView Answer on Stackoverflow
Solution 2 - JavaCrossle SongView Answer on Stackoverflow
Solution 3 - JavaYogesh Umesh VaityView Answer on Stackoverflow