E/RecyclerView﹕ No adapter attached; skipping layout

蹲街弑〆低调 提交于 2019-12-12 04:13:02

问题


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.


回答1:


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.




回答2:


Did you try adding LayoutManager in your recyclerView?

Make sure you call setLayoutManager, like below.

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

before setting adapter into recyclerView, otherwise it is not gonna work.

Source : - recyclerview-not-call-any-adapter-method



来源:https://stackoverflow.com/questions/38105956/e-recyclerview-no-adapter-attached-skipping-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!