下面是从The Bid nerd ranch
这本书中复制过来的,我的问题是关于下面显示的private static class
,我可以更改private static class RunCursorAdapter
到 public class RunCursorAdapter
并将其移动到另一个文件?当加载器因任何原因重新运行时加载器重新加载时,这会导致任何内存问题吗?
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);
}
}
最佳答案
您可以将私有(private)静态类移动到单独的文件中并将其公开。不会有内存问题,因为该类将在一个单独的文件中,它的对象不会对任何内容有任何隐式引用。
代码将以相同的方式工作,您只需将类移到外面即可。如果类不是静态的,情况会有所不同,但在这种情况下是一样的。
关于java - 私有(private)静态类和 CursorLoader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003461/