Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 回收器视图不显示任何项目_Java_Android_Android Recyclerview - Fatal编程技术网

Java 回收器视图不显示任何项目

Java 回收器视图不显示任何项目,java,android,android-recyclerview,Java,Android,Android Recyclerview,我试图在片段中显示联系人,但回收器视图不显示任何项目。尽管列表中有项目,但回收器视图不会显示任何项目。我不确定我做错了什么,因为应用程序在运行时不会崩溃 这是密码 日志 知道为什么会发生这种情况吗?似乎您忘记设置适配器了 在onCreateView方法中,放置如下内容: recyclerView.setAdapter(new UsersAdapter(this.getActivity(), getContactList())); UPD: 在此之前,让getContactList返回相关数据集

我试图在片段中显示联系人,但回收器视图不显示任何项目。尽管列表中有项目,但回收器视图不会显示任何项目。我不确定我做错了什么,因为应用程序在运行时不会崩溃

这是密码

日志


知道为什么会发生这种情况吗?

似乎您忘记设置适配器了

在onCreateView方法中,放置如下内容:

recyclerView.setAdapter(new UsersAdapter(this.getActivity(), getContactList()));
UPD:
在此之前,让getContactList返回相关数据集。初始化回收器视图后不要设置适配器。

奇怪的是,我最近遇到了这个问题,我也使用了全局列表变量,并且列表不是空的,即使Adpeter不是空的,但是直到第二个Adpeter.datasNotifytChanged,RecyclerView才显示任何数据

我在这个网站上找到了一个答案,说创建一个新的列表变量,它就成功了

尝试将actualUserList设置为局部变量,如onDataChange中所示

如果这不能解决您的问题,您也可以在将适配器设置为adapter.notifyDataSetChanged;后尝试调用notifyDataSetChanged


另外,在onCreatedView中设置适配器,并在此onDataChange中调用adapter.notifyDataSetChanged。

看起来您没有调用SetAdapter我已经在getActualUser方法中设置了适配器。您不应该在该方法中设置适配器,这实际上是一个设计缺陷。我建议您在onCreateView或onViewCreated Directly中设置它。每当我打开friends fragment时,我会注意到一件事,它会显示项目一秒钟,然后消失。我已经添加了日志,请检查它日志行告诉您没有连接适配器,跳过布局。这显然意味着您应该尽早设置适配器,使其在本地工作。但若我在onCreatedView中调用setAdapter方法,那个么我有两到三次切换片段来显示项目。@HarshAshra实际上你们并没有,notifyDataSetChanged可以为你们管理更新回收器中的项目
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {
    Context mContext;
    List<Users> mList;

    public UsersAdapter(Context mContext, List<Users> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.display_contacts, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final Users users = mList.get(position);
        holder.name.setText(users.getUsername());
        holder.number.setText(users.getPhoneNumber());
        String url = users.getProfilephotoURL();
        if (url.equals(""))
            holder.circleImageView.setImageResource(R.drawable.ic_user);
        else
            Glide.with(mContext).load(url).into(holder.circleImageView);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ChatActivity.class);
            intent.putExtra("name", users.getUsername());
            intent.putExtra("firendPhoneNumber", users.getPhoneNumber());
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, number;
        CircleImageView circleImageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.contactName);
            number = itemView.findViewById(R.id.contactNumber);
            circleImageView = itemView.findViewById(R.id.profilePic);
        }
    }
}
2020-07-27 14:52:51.651 25366-25366/com.mycompany.newchatapp D/contact:: 167

2020-07-27 14:52:51.670 25366-25366/com.mycompany.newchatapp E/RecyclerView: No adapter attached; skipping layout

2020-07-27 14:52:51.691 25366-25366/com.mycompany.newchatapp W/ClassMapper: No setter/field for status found on class com.mycompany.newchatapp.Model.Users

2020-07-27 14:52:51.691 25366-25366/com.mycompany.newchatapp D/actual:: 1

2020-07-27 14:52:51.723 25366-25366/com.mycompany.newchatapp W/ClassMapper: No setter/field for status found on class com.mycompany.newchatapp.Model.Users

2020-07-27 14:52:51.723 25366-25366/com.mycompany.newchatapp D/actual:: 1
recyclerView.setAdapter(new UsersAdapter(this.getActivity(), getContactList()));
public void onDataChange(@NonNull DataSnapshot snapshot) {
    List<Users> actualuserList = new ArrayList<>();
    if (snapshot.exists()) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            Users user = dataSnapshot.getValue(Users.class);
            actualuserList.add(user);
        }
        Log.d("actual: ", String.valueOf(actualuserList.size()));
        adapter = new UsersAdapter(getContext(), actualuserList);
        recyclerView.setAdapter(adapter);
    }
}