问题
I want to use CursorLoaders
to load date from database but the problem is i dont want to handle a CursorAdapter or it decants i want to have the Cursor
as it is .
here is what i did
class MyClass implements LoaderCallbacks<Cursor> {
Cursor mCursor;
...
...
...
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
if (type == V_TYPE_PART) {
// load all parts
return new CursorLoader(getActivity(),
PartTableMetaData.CONTENT_URI,
PartTableMetaData.TABLE_COLUMNS, null, null,
PartTableMetaData._ID + " ASC");
}
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
counterPartCursor = arg1;
//update the UI
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mCursor= null;
}
}
Now this work fine . My Question is i am doing it the right way ? or should i make something else like closing the cursor in the onLoaderReset
??
回答1:
No closing the cursor is not required.setting null will make the things work as CursorLoader will take care of closing it. But I can see the leakage in your current code as
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
counterPartCursor = arg1;
//update the UI
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mCursor= null;
}
you are holding reference in counterPartCursor and making mCursor null !!!
来源:https://stackoverflow.com/questions/15912512/using-loaders-with-cursors