Recyclerview Error: No Adapter Attached; Skipping Layout
When I try to add RecyclerView in the sample application, I get an error E/RecyclerView: No adapter attached; skipping layout. Because of this, the data which is successfully retr
Solution 1:
Getting data from internet is an async
task and may takes too long, here you don't bind any adapter
to the recyclerView
until you get a response and this is not the usual right way, If you don't get any response for some reasons (e.g no internet connection), then recyclerView.setAdapter
won't run, So I suggest you to create an empty adapter first, Then whenever you get response just update the adapter
:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(newLinearLayoutManager(getActivity()));
//Pass empty list to (predefined) adapter
adapter = newChannelAdapter (newArrayList<>(), rowLayout, getActivity());
//Bind it to recyclerView
recyclerView.setAdapter(adapter);
getChannelData();
// Inflate the layout for this fragmentreturn rootView;
}
Add some method to your adapter in case you want add Channel
:
public void addChannel(Channel channel) {
channels.add(channel);
notifyDatasetChanged();//even better notifyItemInserted();
}
Now in your response listener add the response body that you got to the adapter:
@OverridepublicvoidonResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
//Use a loop to add channels you got
adapter.addChannel(responsebodyItems);
}
Post a Comment for "Recyclerview Error: No Adapter Attached; Skipping Layout"