Search code examples
androidandroid-recyclerviewfirebase-realtime-databasefirebaseui

E/RecyclerView﹕ No adapter attached; skipping layout


By the title of this question, its easily understandable that, the adapter of recyclerview is not set inside a UI thread. But in my case to avoid that I have tried doing it even on UI thread but still no luck.

I am using FirebaseUI for my app. Below is the code snippet:

public static void getUserFromUserId(@NonNull DatabaseReference dbRef, @NonNull String userId) {
    dbRef.child(USERS).child(userId)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    FriendsActivity.this.runOnUiThread(new Handler() {
                        @Override
                        public void run() {
                            loadFriends(user);
                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    FirebaseCrash.report(databaseError.toException());
                }
            });
}

private void loadFriends(User user) {
    Query friendsRef = ;  // your firebase DatabseReference path
    FirebaseRecyclerAdapter<Friend, FriendViewHolder> adapter =
            new FirebaseRecyclerAdapter<Friend, FriendViewHolder>(Friend.class,
                    R.layout.item_challenge, FriendViewHolder.class, friendsRef) {
                @Override
                protected void populateViewHolder(FriendViewHolder viewHolder, Friend model, int position) {
                    viewHolder.setFriendName(model.getName());
                    viewHolder.setFriendPic(FriendActivity.this, model.getProfilePic());
                }
            };
    mRecyclerView.setAdapter(adapter);
}

My Activity's onCreate() method has below code related to RecyclerView:

mRecyclerView = (RecyclerView) findViewById(R.id.challenge_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

I dont understand why even after calling loadFriends inside runOnUiThread, why the error still persists.

Any help is appreciated.


Solution

  • You need to attach your adapter when you initialize your RecyclerView and attach LayoutManager, etc.

    loadFriends method should fetch data and add data to the adapter and then you should call notifyDataSetChanged or equivalent.

    What you're doing here is incorrect. A recyclerview should always have an adapter attached. You just need to update the data in the adapter.

    And that's what the error says E/RecyclerView﹕ No adapter attached; skipping layout. Because you have not attached adapter after attaching LayoutManager and you're attaching adapter at a later stage.