You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android RecyclerView无法显示数据,请求排查问题原因

RecyclerView加载数据为空的问题排查与解决

核心问题

你的RCAdapter类中getItemCount()方法硬返回了0,RecyclerView依赖这个方法确定列表项的总数量,返回0会让RecyclerView认为没有数据需要渲染,因此列表始终为空。

修复方案

修改RCAdapter中的getItemCount()方法,返回列表的实际长度:

@Override
public int getItemCount() {
    // 增加非空判断,避免空指针异常
    return rcsubjectList != null ? rcsubjectList.size() : 0;
}

额外优化建议

  1. 给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;
}
  1. 在Adapter的构造方法中对传入的列表做非空处理,避免后续操作出现空指针:
public RCAdapter(List<RCModelClass> rcsubjectList){
    this.rcsubjectList = rcsubjectList != null ? rcsubjectList : new ArrayList<>();
}

内容的提问来源于stack exchange,提问作者Milutin Nedeljkovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 07:45:02