-4

I have added items to RecyclerView and set adapter but it is showing zero elements. Means the list is empty. Please check and help me

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);

    }
  }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Suheb Ip
  • 25
  • 2
  • 2
    In getItemCount your returning 0, return your chatList size – Bharath Kumar Sep 26 '17 at 12:24
  • @BharathKumar please don't answer in comment specially when the answer has been already posted – Pavneet_Singh Sep 26 '17 at 12:29
  • 5
    @Pavneet_Singh and for you, please don't answer duplicates – Tim Sep 26 '17 at 12:31
  • @TimCastelijns i knew someone would come by and say that( but you should have advised both , equality ?) and i follow it many time, just got little existed for with the quicky , BTW the dupe answer had syntax error , fixed that for OP too – Pavneet_Singh Sep 26 '17 at 12:35
  • "i knew someone would come by and say that" - yet you still answer it, creating duplicate content – Tim Sep 26 '17 at 12:39
  • with due respect, i must say we can have a debate on this , frankly it's been a debate, people understand this when they are open to understand both sides but yet you are only advising me (you like me , i know , i too but as pals only) , i will be more careful , thanks – Pavneet_Singh Sep 26 '17 at 12:45

6 Answers6

2

return size of arraylist in getitemcount.

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

Inside your getItemCunt you are returning 0

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

instead of returning 0 return list size which is chatList

@Override
public int getItemCount() {
    return chatList.size();
}
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
1

Replace return 0 by return chatList.size();

@Override
public int getItemCount() {
    return chatList.size();
}
Ankita
  • 1,129
  • 1
  • 8
  • 15
1

use this

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

insted of this

@Override
public int getItemCount() {
    return 0;
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Try this in adapter class;

@Override
 public int getItemCount() {
    return chatList.size();
 }
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
1

Try

Return size of list if list is not null otherwise 0

@Override
public int getItemCount() {
   return chatList == null ? 0 : chatList.size();
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53