Sunday, May 17, 2015

Handling adapter error while using RecyclerView in Android

Standard
Hello All:

RecyclerView is introduced with Android L and is also available with AppCompat library. RecyclerView provides better layout option and performance as compared to ListView. However, in this post I will share one of the possible solutions to a problem which occurs with RecyclerView.
Often in our application we rely on network data to be populated in our views. With RecyclerView one problem occurs when the data is not available during initialization.

In the logcat, you can often see following error.
recyclerview No adapter attached; skipping layout


One way to fix this issue is to attach an empty adapter to the RecyclerView

    void initializeRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
        recyclerView.setHasFixedSize(true);
    }
 
Call this method in OnCreateView or OnCreate methods.
Hope this helps. Let me know if you know any other way of fixing the issue.

Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

4 comments :

  1. Why does this problem occur? any docs supporting this?

    ReplyDelete
  2. this is error should come while using retrofit API

    ReplyDelete
  3. Would this be applicable to volley?

    ReplyDelete
  4. would this be applicable to volley?

    ReplyDelete