Android RecyclerView无法显示数据,请求排查问题原因
RecyclerView加载数据为空的问题排查与解决
核心问题
你的RCAdapter类中getItemCount()方法硬返回了0,RecyclerView依赖这个方法确定列表项的总数量,返回0会让RecyclerView认为没有数据需要渲染,因此列表始终为空。
修复方案
修改RCAdapter中的getItemCount()方法,返回列表的实际长度:
@Override public int getItemCount() { // 增加非空判断,避免空指针异常 return rcsubjectList != null ? rcsubjectList.size() : 0; }
额外优化建议
- 给
RCModelClass的构造方法添加public修饰符,确保跨包场景下也能正常实例化对象:
public RCModelClass(String subject_text, String time_text, String classroom_text, String teacher_text){ this.subject_text = subject_text; this.time_text = time_text; this.classroom_text = classroom_text; this.teacher_text = teacher_text; }
- 在Adapter的构造方法中对传入的列表做非空处理,避免后续操作出现空指针:
public RCAdapter(List<RCModelClass> rcsubjectList){ this.rcsubjectList = rcsubjectList != null ? rcsubjectList : new ArrayList<>(); }
内容的提问来源于stack exchange,提问作者Milutin Nedeljkovic
相关产品推荐
相关产品推荐

