Issue
I need help, i have an error, that is this: E/RecyclerView: No adapter attached; skipping layout
CODE:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view= inflater.inflate(R.layout.fragment_chats, container, false);
recyclerView= view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
fuser= FirebaseAuth.getInstance().getCurrentUser();
usersList= new ArrayList<>();
reference= FirebaseDatabase.getInstance().getReference("Chatlist").child(fuser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
usersList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Chatlist chatlist= snapshot.getValue(Chatlist.class);
usersList.add(chatlist);
}
chatList();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
private void chatList() {
mUsers = new ArrayList<>();
reference= FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
User user= snapshot.getValue(User.class);
for(Chatlist chatlist : usersList){
if(user.getId().equals(chatlist.getId())){
mUsers.add(user);
}
}
}
userAdapter= new UserAdapter(getContext(), mUsers, true);
recyclerView.setAdapter(userAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Solution
Try to follow the approach:
- Create an empty adapter the same time you set LayoutManager for the RecyclerView: Save it as field of your Fragment:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
userAdapter= new UserAdapter(getContext(), new ArraysList<>(), true);
recyclerView.setAdapter(userAdapter);
- When data is ready, populate the adapter and notify:
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
/// origin code here
// reset data in adapter and not re-creating adapter:
userAdapter.setItems(mUsers);
getActivity().runOnUiThread(() -> userAdapter.notifyDataSetChanged());
// instead of userAdapter= new UserAdapter(getContext(), mUsers, true); recyclerView.setAdapter(userAdapter);
}
Answered By - S-Sh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.