2

The below copied from the book The Bid nerd ranch , my question is about private static class shown below, Can I change private static class RunCursorAdapter to public class RunCursorAdapter and move it to another file? will that cause any memory issue when the loader reload when loader re-run for any reason?

public class RunListFragment extends ListFragment implements LoaderCallbacks<Cursor> {


@Override

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new RunListCursorLoader(getActivity());
}


@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    RunCursorAdapter adapter =
    new RunCursorAdapter(getActivity(), (RunCursor)cursor);
    setListAdapter(adapter);
}



@Override
public void onLoaderReset(Loader<Cursor> loader) {
    setListAdapter(null);
}


private static class RunCursorAdapter extends CursorAdapter {

    private RunCursor mRunCursor;

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


    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    }


    public void bindView(View view, Context context, Cursor cursor) {

        Run run = mRunCursor.getRun();
        TextView startDateTextView = (TextView)view;
        String cellText = context.getString(R.string.cell_text, run.getStartDate());
        startDateTextView.setText(cellText);

    }

}

}

Hosseini
  • 547
  • 3
  • 15
amar Kumar
  • 23
  • 4

2 Answers2

1

You can move a private static class to a separate file and make it public. There will be no memory issues since the class will be in a separate file, its objects will not have any implicit references to anything.

The code will work the same way, you are simply moving the class outside. It would make a difference if the class wasn't static, but in this case it's the same.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
0

You can move the class to be a top-level class, though it need not be 'public', but by elevating its visibility above 'private' you incur the responsibility never to change its public-facing protocols. Normally you should abhor elevating a member's access. The more other types can access it, the harder it is to maintain.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10