0

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.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125

2 Answers2

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.

Froyo
  • 17,947
  • 8
  • 45
  • 73
0

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

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • Code inside my onCreate() method:mRecyclerView = (RecyclerView) findViewById(R.id.challenge_recycler_view); mRecyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); – Chandra Sekhar Jun 29 '16 at 17:27