【问题标题】:method cannot be resolved: adapter.swapCursor(loader);方法无法解析:adapter.swapCursor(loader);
【发布时间】:2015-08-17 21:13:12
【问题描述】:

我正在开发一个加载 reddits 并将其放入数据库的 Android 应用程序,我在我的片段 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)'

【问题讨论】:

标签: android android-fragments cursor loader


【解决方案1】:

将此代码添加到您的适配器中,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();
        }
    }

【讨论】:

  • 这里重要的是使用了recyclerview,它本身做的很少。您必须添加到适配器。
猜你喜欢
  • 2019-10-27
  • 2017-11-13
  • 2015-11-04
  • 2019-10-17
  • 2018-01-15
  • 1970-01-01
  • 2018-05-24
  • 2020-07-08
  • 2019-01-18
相关资源
最近更新 更多