In my RecyclerView I set the item count to a certain number but whenever I am scrolling in the view it creates more which ends up in an error which says "java.lang.IndexOutOfBoundsException: Index: 15, Size: 15" because it is trying to make another item but runs out of data in the ArrayList. Here is my RecyclerView Adapter code:
public class AnnouncementsAdapter extends
RecyclerView.Adapter<AnnouncementsAdapter.ViewHolder> {
public int firstIndexFinalStep = -1;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public CardView announcementsCards;
public TextView categoryText;
public TextView timePostedText;
public TextView announcementText;
public LinearLayout layout;
public RelativeLayout relativelayout;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
categoryText = itemView.findViewById(R.id.categoryText);
timePostedText = itemView.findViewById(R.id.timePostedText);
announcementText = itemView.findViewById(R.id.announcementText);
announcementsCards = itemView.findViewById(R.id.announcementCardsView);
relativelayout = itemView.findViewById(R.id.relativelayout);
}
}
// Bus Times Adapter constructor
private ArrayList<String> mAnnouncementChannelList;
private ArrayList<String> mAnnouncementCreatedAtList;
private ArrayList<String> mAnnouncementMessagesList;
public AnnouncementsAdapter(ArrayList announcementChannelList, ArrayList announcementCreatedAtList, ArrayList announcementMessagesList) {
mAnnouncementChannelList = announcementChannelList;
mAnnouncementCreatedAtList = announcementCreatedAtList;
mAnnouncementMessagesList = announcementMessagesList;
}
// Usually involves inflating a layout from XML and returning the holder
@NonNull
@Override
public AnnouncementsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
System.out.println("On create " + firstIndexFinalStep);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.announcements, viewGroup, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
//if (firstIndexFinalStep >= mAnnouncementMessagesList.toArray().length - 3) {
// return;
//}
System.out.println("making date thing");
String pattern = "MMM dd, yyyy h:mm a";
DateFormat df = new SimpleDateFormat(pattern);
firstIndexFinalStep += 1;
// Set item views based on your views and data model
TextView categoryText = viewHolder.categoryText;
TextView timePostedText = viewHolder.timePostedText;
TextView announcementText = viewHolder.announcementText;
String categoryString = "";
String timePostedString = "";
String announcementTextString = "";
System.out.println(mAnnouncementMessagesList.toArray().length);
System.out.println(firstIndexFinalStep);
categoryString = mAnnouncementChannelList.get(this.firstIndexFinalStep);
timePostedString = "Posted " + df.format(mAnnouncementCreatedAtList.get(this.firstIndexFinalStep));
announcementTextString = mAnnouncementMessagesList.get(this.firstIndexFinalStep);
categoryText.setText(categoryString);
timePostedText.setText(timePostedString);
announcementText.setText(announcementTextString);
System.out.println(categoryText);
System.out.println(timePostedText);
System.out.println(announcementText);
CardView cardview = viewHolder.announcementsCards;
LinearLayout layout = viewHolder.layout;
RelativeLayout relativelayout = viewHolder.relativelayout;
}
@Override
public int getItemCount() {
return mAnnouncementMessagesList.toArray().length;
}
}
Basically I want the RecyclerView to act as a ListView by loading all the items then stop loading new items or recycling items.
Thanks for the help!