开发者

Android ListFragment Cursor managing

开发者 https://www.devze.com 2023-02-15 23:12 出处:网络
I want to use ListF开发者_StackOverflow社区ragment and Loader Class for my android app. (honeycomb level SDK, but Google release comparability package)

I want to use ListF开发者_StackOverflow社区ragment and Loader Class for my android app. (honeycomb level SDK, but Google release comparability package)

in Fragment, is it has managing cursor API with Fragmemt life cycle

Activity has "startManagingCursor(Cursor c)"

or

Fragment automatically manage cursor in its life cycle?


I'm not sure I got your question right, but if you're asking if your cursor is managed automaticaly in a ListFragment when using a Loader, then I believe the answer is: yes, it is.

In your ListFragment you would implement the LoaderManager.LoaderCallbacks interface, and then use initLoader() and restartLoader() from LoaderManager to manage your data and the cursor during the ListFragment's lifecycle. The underlying adatpter must support the swapCursor() method for this to work best.

Something along the lines of:


class Foo extends ListFragment implements LoaderManager.LoaderCallbacks {

    private MyAdapter   mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new MyAdapter();
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        Uri MY_URI = "your data URI goes here";
        return new CursorLoader(getActivity(), MY_URI, MY_PROJECTION, selection, selectionArgs, sortOrder);
    }

    @Override
    public void onLoadFinished(Loader loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader loader) {
        mAdapter.swapCursor(null);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号