Recyclerview显示0项

时间:2017-09-26 12:22:51

标签: android

我已将项目添加到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);

    }
  }
}

6 个答案:

答案 0 :(得分:2)

在getitemcount中返回arraylist的大小。

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

答案 1 :(得分:1)

getItemCunt内,您将返回0

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

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

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

答案 2 :(得分:1)

返回0 替换为返回chatList.size();

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

答案 3 :(得分:1)

使用此

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

这是

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

答案 4 :(得分:1)

在适配器类中尝试此操作;

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

答案 5 :(得分:1)

尝试

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

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