0

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!

Jacks
  • 803
  • 1
  • 6
  • 16
Leafs5
  • 1
  • 1
  • a minimum reproducible example would make this digestible. its not so much fun starting at a ton of code with things that aren't really relevant to the question – William Reed Nov 23 '19 at 04:54
  • The `RecyclerView` is only creating enough `View`s and `ViewHolder`s to fill the screen. That's not the problem. It's more likely that your three `ArrayList`s don't have the same number of elements in each. Please [edit] your question to provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Nov 23 '19 at 05:18
  • 1
    First learn how to use Recyclerview.Adapter. Why are you using `firstIndexFinalStep`? Use the index value given by `onBindViewHolder` method. – Sourav Bagchi Nov 23 '19 at 05:45
  • Yep, Sourav Bagchi's got it. `firstIndexFinalStep` is always increasing, every time `onBindViewHolder()` is called, which will eventually happen more often than you have items in those lists. – Mike M. Nov 23 '19 at 06:00
  • Try to change the logic in the adapter initialization where you are passing 3 different ArrayLists - try to make a common Java Object to pass to the adapter constructor so that the size will become constant, even though if you have blank value inserted. – Jacks Nov 23 '19 at 06:30

0 Answers0