方法无法解析:adapter.swapCursor(loader);

时间:2015-08-17 21:13:12

标签: android android-fragments cursor loader

我正在开发一个Android应用程序,它加载reddits并将其放入db中,我在我的片段SubredditsFragment.class中使用了异步游标加载器。该片段包含一个适配器,它有一个游标加载器。当我停止或重置加载器时,需要在我的适配器上交换加载器。

    public class SubRedditsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {


        private List<SubRedditData> subRedditDataList;

        private IntentFilter filter;

        public static final String TAG = SubRedditsFragment.class.getName();

        private SubredditAdapter adapter;
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            String[] projection = null;
            String where = null;
            String[] whereArgs = null;
            String sortOrder = null;

            Uri queryUri = RedditContentProvider.CONTENT_URI;

            return new CursorLoader(getActivity(), queryUri, projection, where, whereArgs, sortOrder);
        }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG,"Added broadcastreceiver");
        getActivity().registerReceiver(receiver,filter);
        adapter = new SubredditAdapter(getActivity().getApplicationContext(),subRedditDataList);    

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);

            getLoaderManager().destroyLoader(loader.getId());
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            adapter.swapCursor(null);
        }

The problem is that I can't use the method adapter.swapCursor(), it's unknown for Android. I get the error message Cannot resolve method 'swapCursor(loader)'

1 个答案:

答案 0 :(得分:0)

在适配器中添加此代码,mCursor是全局游标变量

public void swapCursor(Cursor newCursor) {
        // Always close the previous mCursor first
        if (mCursor != null) mCursor.close();
        mCursor = newCursor;
        if (newCursor != null) {
            // Force the RecyclerView to refresh
            this.notifyDataSetChanged();
        }
    }