Reputation: 113
I creating a chatting app with using firebase in android studio , all my activity working properly expect my date display,
I wanted to set date at once if it is same otherwise show new dates if it is not same to previous date , I was trying to set but it show some errors
Process: com.example.registration, PID: 13007
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at com.example.registration.myadapter.onBindViewHolder(myadapter.java:49)
at com.example.registration.myadapter.onBindViewHolder(myadapter.java:19)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
here is the error
and I fetch the current date
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = sdf.format(new Date());
and then give it to the chat.java using Button.OnclickListner
sendmsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textmsg=inputmsg.getText().toString();
if(!textmsg.equals("")){
sendmessage(firebaseUser.getUid(),userref,textmsg,currentDate,currenttime);
}
else{
Toast.makeText(chatscreen.this, "ENTER SOMETHING TO SEND MESSAGE", Toast.LENGTH_SHORT).show();
}
inputmsg.setText("");
}
});
and here is my adapter
@Override
public void onBindViewHolder(@NonNull @NotNull myadapter.ViewHolder holder, int position) {
Chat name=values.get(position);
Chat pname=values.get(position - 1);//line 49
holder.txtmsg.setText(name.getMessage());
// holder.date.setText(name.getDate());
if(!name.getDate().equals(pname.getDate())) {
holder.time.setText(name.getTime());
}
else{
holder.date.setVisibility(View.INVISIBLE);
}
if(position==values.size()-1){
if(name.getIsseen()){
holder.textseen.setText("SEEN");
}
else{
holder.textseen.setText("DELIVERED");
}
}
}
@Override
public int getItemCount() {
return values.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtmsg;
public TextView date;
public TextView time;
public TextView textseen;
public View log;
public ViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
txtmsg=(TextView) itemView.findViewById(R.id.usermsg);
date=(TextView) itemView.findViewById(R.id.date);
time=(TextView) itemView.findViewById(R.id.time);
textseen=(TextView) itemView.findViewById(R.id.isseen);
}
}
anyone know how to fix this ?
Upvotes: 0
Views: 334
Reputation: 12953
For the first item in RecyclerView
, position
will be 0
which makes position - 1 = -1
which is an invalid index hence the exception, in onBindViewHolder
check for position > 0
before doing -1
@Override
public void onBindViewHolder(@NonNull @NotNull myadapter.ViewHolder holder, int position) {
Chat name=values.get(position);
holder.txtmsg.setText(name.getMessage());
if(position > 0)
Chat pname=values.get(position - 1);
if(!name.getDate().equals(pname.getDate())) {
holder.time.setText(name.getTime());
}else{
holder.date.setVisibility(View.INVISIBLE);
}
}
if(position==values.size()-1){
if(name.getIsseen()){
holder.textseen.setText("SEEN");
}else{
holder.textseen.setText("DELIVERED");
}
}
}
Upvotes: 1