@blog.justoneplanet.info

日々勉強

AndroidでCursorLoaderを使ってみる

以下のようにしてCursorLoaderを使用する。

public class MainFragment extends ListFragment
    implements LoaderManager.LoaderCallbacks<Cursor> {
    private ListView mListView;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mListView = getListView();
        mListView.setCacheColorHint(0);
        mAdapter = new EmoticonSimpleCursorAdapter(
                getActivity().getApplicationContext(),
                R.layout.list,
                null,
                new String[]{Table.TITLE, Table.CONTENT},
                new int[]{R.id.list_title, R.id.list_content},
                this
        );
        setListAdapter(mAdapter);
        super.onViewCreated(view, savedInstanceState);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getLoaderManager().destroyLoader(0);
    }
    private void reload() {
        getLoaderManager().restartLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity().getApplicationContext(), Provider.URI, null, null, null, Table.CREATED + " DESC");
    }
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment