Im having an issue with my recycler view or more accurately with a secondary arraylist used to set a value in each viewholder.
I have a list of items sent to my adapter and in its constructor I create an arraylist the size of this list. An arraylist of items works correctly but the second arraylist of integers containing all ones says outofbounds. A quick logd shows thats not the case and that there is a list of 1's there but everytime i try to access this its outofbounds at index 0.
Class def
RecyclerView recyclerView;
ItemAdapter itemAdapter;
List<Item> items;
List<Boolean> itemsSelected;
List<Integer> quantities;
OnCreate
quantities = new ArrayList<>();
Method to create list of items for adapter and also create equal length list of 1's
for (int i = 0; i <categories.size() ; i++) {
long length = dataSnapshot.child(categories.get(i)).getChildrenCount();
Iterator<DataSnapshot> toAdd= dataSnapshot.child(categories.get(i)).getChildren().iterator(); // this returns all the items in each category
for (int j = 0; j <length ; j++) {
DataSnapshot newData = toAdd.next();
items.add(newData.getValue(Item.class));
//quantities.add(Integer.valueOf(1));
//itemsSelected.add(Boolean.valueOf(false));
Log.d(TAG, "onDataChange: debug: "+ newData.toString());
}
}
for (int i = 0; i < items.size() ; i++) {
quantities.add(i,Integer.valueOf(1));
itemsSelected.add(i,Boolean.valueOf(false));
}
itemAdapter = new ItemAdapter(items);
itemAdapter.notifyDataSetChanged();
In the above code the two lines commented out are another attempt at creating a list of ones but it also had the same error so i moved it to a bunch of other places.
I also do the same here in the adapter constructor
public ItemAdapter(List<Item> items){
this.items = items;
for (int i = 0; i < items.size() ; i++) {
quantities.add(i,Integer.valueOf(1));
itemsSelected.add(i,Boolean.valueOf(false));
Log.d(TAG, "ItemAdapter: quan "+ quantities.get(i));
Log.d(TAG, "ItemAdapter: bool "+ itemsSelected.get(i));
}
Logd shows placement Typical log output
ItemAdapter: quan 1
bindItem: quan [1, 1, 1, 1, 1, 1, 1, 1]
What am i doing wrong. The list exists even when binding item because if i logd the whole list instead of a single index the whole list is outputted but get(index) causes outofbounds.
Thanks for taking the time to look at this.
EDIT: Adapter code
private class ItemAdapter extends RecyclerView.Adapter<ItemHolder>{
List<Item> items;
Item item;
public ItemAdapter(List<Item> items){
this.items = items;
for (int i = 0; i < items.size() ; i++) {
quantities.add(i,Integer.valueOf(1));
itemsSelected.add(i,Boolean.valueOf(false));
Log.d(TAG, "ItemAdapter: quan "+ quantities.get(i));
Log.d(TAG, "ItemAdapter: bool "+ itemsSelected.get(i));
}
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view= layoutInflater.inflate(R.layout.singleitem,parent,false);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
item = items.get(position);
holder.bindItem(item,position);
}
@Override
public int getItemCount() {
return items.size();
}
}