RecyclerView No adapter attached; skipping layout, Data not showing

懵懂的女人 提交于 2019-12-07 09:52:29

Try setting adapter in onLoadFinished() and also use getActivity() for context in adapter object

@Override
public void onLoadFinished(Loader<List<Song>> loader, List<Song> data) {
    mSongs = data;
     mSongListAdapter = new SongListAdapter(getActivity(), mSongs);
    mRecyclerView.setAdapter(mSongListAdapter);
}

also in this mRecyclerView = new FastScrollRecyclerView(getContext()); to

mRecyclerView = new FastScrollRecyclerView(getActivity());

Basically use getActivity() for context in fragment class

Ugh! I wasted a lot of time in this but finally came out with the solution. I removed Butterknife binding and used conventional findViewById inside onCreateView() of SongsFragment after capturing the view instance by changing the onCreateView() to this:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
mRecyclerView = (FastScrollRecyclerView) rootView.findViewById(R.id.rvSongs);
//Rest of the things
}

Turns out I was using ButterKnife the wrong way so the instance mRecyclerView was null but later with line mRecyclerView = new FastScrollRecyclerView(getContext()); it wasn't null anymore but it still wasn't connected to the view so I didn't get NullPointerException and the code didn't work.

I know it was a noob mistake :D

Correct way to use ButterKnife with fragments as picked up from official website is:

public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!