Using Loaders with Cursors

坚强是说给别人听的谎言 提交于 2019-12-24 12:28:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!