gpt4 book ai didi

java - E/RecyclerView 错误 : No adapter attached; skipping layout

转载 作者:行者123 更新时间:2023-12-02 01:07:14 25 4
gpt4 key购买 nike

I want to get a list of registered users in UserFragment, actually here is my code, but when I go to the "Users" tab I get an error: E/RecyclerView: No adapter attached; skipping layout

UsersFragment.java

public class UsersFragment extends Fragment {


RecyclerView recyclerView;
AdapterUsers adapterUsers;
List<ModelUser> userList;

FirebaseAuth firebaseAuth;

public UsersFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_users, container, false);

firebaseAuth = FirebaseAuth.getInstance();

recyclerView = view.findViewById(R.id.users_recyclerView);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

userList = new ArrayList<>();

getAllUsers();

return view;
}

private void getAllUsers() {
final FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userList.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()){
ModelUser modelUser = ds.getValue(ModelUser.class);

if (!modelUser.getUid().equals(fUser.getUid())){
userList.add(modelUser);
}

adapterUsers = new AdapterUsers(getActivity(), userList);
recyclerView.setAdapter(adapterUsers);

}

}

what am I doing wrong?

最佳答案

在构建用户列表后,在 for 循环之外执行此部分:

adapterUsers = new AdapterUsers(getActivity(), userList);
recyclerView.setAdapter(adapterUsers);

或者,在 onCreate 中,您可以使用空的 userList 设置回收器 View 的适配器,然后在 getAllUsers() 末尾设置 函数,并在 adapterUsers 中更新新的 userList,然后调用 notifyDataSetChanged()

关于java - E/RecyclerView 错误 : No adapter attached; skipping layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com