我在android studio中使用firebase创建了一个聊天应用程序,我所有的活动都正常工作,除了我的日期显示,
我想立即设置日期,如果它是相同的,否则显示新的日期,如果它是不相同的前一个日期,我试图设置,但它显示一些错误
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)
这里是错误
和我获取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = sdf.format(new Date());
,然后把它交给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("");
}
});
这是适配器
@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);
}
}
谁知道怎么解决这个问题?
对于RecyclerView
中的第一项,position
将是0
,这使得position - 1 = -1
是一个无效的索引,因此例外,在onBindViewHolder
中,在执行-1
之前检查position > 0
@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");
}
}
}