Why is the cursor saved in RunListFragment.RunCursorAdapter?


#1

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

[code]
private static class RunCursorAdapter extends CursorAdapter {

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

    @Override
    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());
    }[/code]

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

Ellen


#2

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.