Why is the cursor saved in RunListFragment.RunCursorAdapter?


The constructor for RunListFragment.RunCursorAdapter takes and saves a RunCursor. The only place it is used is in its bindView() method:

private static class RunCursorAdapter extends CursorAdapter {

    private RunCursor mRunCursor;
    public RunCursorAdapter(Context context, RunCursor cursor) {
        super(context, cursor, 0);
        mRunCursor = cursor;

    public void bindView(View view, Context context, Cursor cursor) {
        // get the run for the current row
       Run run = mRunCursor.getRun();  // Could use ((RunCursor cursor).getRun()
        // set up the start date text view
        TextView startDateTextView = (TextView)view;
        startDateTextView.setTextColor(RunManager.get(mContext).isTrackingRun(run) ? Color.RED : Color.BLACK);
        startDateTextView.setText("Run at " + run.getStartDate());

Why is mCursor needed? Couldn’t bindView() use the cursor that’s passed in as a parameter, as shown in the above comment?



Realize this is an older post, but to answer your question, yes, you could use the Cursor that gets passed in to bindView of the adapter class, but quoting from pg. 552 of the book:

Perhaps, six of one and half-dozen of the the other, but personally feel using the instance variable makes for cleaner reading code.