本文介绍了错误 ANDROID STUDIO E/RecyclerView:未连接适配器;跳过布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助,我有一个错误,那就是:E/RecyclerView:没有连接适配器;跳过布局
I need help, i have an error, that is this: E/RecyclerView: No adapter attached; skipping layout
代码:
公共视图 onCreateView(LayoutInflater inflater, ViewGroup 容器,捆绑savedInstanceState) {查看 view= inflater.inflate(R.layout.fragment_chats, container, false);
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) {
}
});
}
}
推荐答案
尝试遵循以下方法:
- 在为 RecyclerView 设置 LayoutManager 的同时创建一个空适配器:将其另存为 Fragment 的字段:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
userAdapter= new UserAdapter(getContext(), new ArraysList<>(), true);
recyclerView.setAdapter(userAdapter);
- 数据准备好后,填充适配器并通知:
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);
}
这篇关于错误 ANDROID STUDIO E/RecyclerView:未连接适配器;跳过布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!