【问题标题】:Recyclerview showing 0 items [duplicate]Recyclerview 显示 0 个项目 [重复]
【发布时间】:2017-09-26 12:22:50
【问题描述】:

我已将项目添加到 RecyclerView 并设置适配器,但它显示零元素。表示列表为空。请检查并帮助我

ChatAdapter.java

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  Context context;
  ArrayList<ChatModel> chatList;

  public ChatAdapter(Context context, ArrayList<ChatModel> chatList) {
    this.context = context;
    this.chatList = chatList;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new TextInViewHolder(LayoutInflater.from(context).inflate(R.layout.chat_list_in_text, parent, false));
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    TextInViewHolder hold = (TextInViewHolder) holder;
    hold.tvIncText.setText(chatList.get(position).getText());

  }

  @Override
  public int getItemCount() {
    return 0;
  }

  public class TextInViewHolder {

    public TextInViewHolder(View itemView) {
        super(itemView);

    }
  }
}

【问题讨论】:

  • 在getItemCount中你返回0,返回你的chatList大小
  • @BharathKumar 请不要在答案已经发布后在评论中特别回答
  • @Pavneet_Singh 和你,请不要重复回答
  • @TimCastelijns 我知道有人会过来说(但你应该同时建议两者,平等?)我遵循它很多次,只是因为快速,顺便说一句骗人的答案几乎不存在有语法错误,也为 OP 修复了这个问题
  • “我知道有人会过来这么说” - 但你仍然回答它,创建重复的内容

标签: android


【解决方案1】:

在 getitemcount 中返回数组列表的大小。

public int getItemCount() {
return chatList.size();}

【讨论】:

    【解决方案2】:

    在您的getItemCunt 中,您返回 0

    @Override
    public int getItemCount() {
        return 0;
    }
    

    而不是返回 0 返回列表大小,即 chatList

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

    【讨论】:

      【解决方案3】:

      return 0 替换为 return chatList.size();

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

      【讨论】:

        【解决方案4】:

        使用这个

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

        这个

        @Override
        public int getItemCount() {
            return 0;
        }
        

        【讨论】:

          【解决方案5】:

          在适配器类中试试这个;

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

          【讨论】:

            【解决方案6】:

            试试

            如果列表不为空,则返回列表的大小,否则为 0

            @Override
            public int getItemCount() {
               return chatList == null ? 0 : chatList.size();
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-25
              • 2021-12-20
              • 1970-01-01
              相关资源
              最近更新 更多