tsiro
tsiro

Reputation: 2393

No adapter attached; skipping layout when using cursor loader

I would like to populate RecyclerView in onLoadFinished() with the cursor data returned from a CursorLoader. The RecyclerView is populated as it is expected, but I am still getting the same error "No adapter attached; skipping layout"

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    recyclerViewAdapter = new RecyclerViewAdapter(getApplicationContext(), data);

    //attach Layout Manager to RecyclerView
    recyclerView.setLayoutManager(newLinearLayoutManager(getApplicationContext()));  

    //attach adapter to RecyclerView
    recyclerView.setAdapter(recyclerViewAdapter);

}

Upvotes: 0

Views: 2137

Answers (3)

tsiro
tsiro

Reputation: 2393

Finally after a couple of days i managed to figure out the issue...what i did was to create an instance of my RecyclerView Adapter inside my onActivityCreated passing in only the context of my Fragment to the Adapter's constructor and set the Adapter to my RecyclerView

recyclerViewAdapter = new RecyclerViewAdapter(getActivity());
recyclerView.setAdapter(recyclerViewAdapter);

So, the RecyclerView Framework has an adapter attached and thus no layout was skipping. The cursor data ara being passed(swapped) to the RecyclerView Adapter from the onLoadFinished() and onLoadReset() methods of the CursorLoader respectively

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    recyclerViewAdapter.swapCursor(data);
}

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

the swapCursor method has been customed implemented inside my RecyclerView Adapter class

Upvotes: 1

Thilek
Thilek

Reputation: 676

This error message shown when the view is created. There is a recyclerview but not adapter was set. I will suggest to create and set the adapter to recyclerview onCreate or in onCreateView (if fragment). Then just set the cursor or data to the adapter onLoadFinished and notify your adapter for data change..

or just ignore the error message.. No harm done by the error message ;-)

Upvotes: 0

Bryan Herbst
Bryan Herbst

Reputation: 67229

Regardless of when you attach an adapter, your RecyclerView is going need to lay itself out on the screen as soon as the containing Activity/Fragment needs to layout- that is, when the screen is first displayed to the user.

That means that before your CursorLoader has finished loading, your layout will likely be displayed to the user. For this to happen, the RecyclerView will need to query its adapter to figure out what to display.

You should instead set your layout manager and an empty adapter when your Activity/Fragment is first created, then update the contents of the adapter once your cursor finishes loading.

Upvotes: 0

Related Questions