0

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();
    }


}
ah4444
  • 1
  • 3

1 Answers1

0

How are you accessing the quantities list in your adapter?

A way to solve this can be passing the quantities list into the constructor of the adapter and then calling the notifyDataSetChanged() method, after adding values to both the lists (remember adding & not reseting the list). This would ensure that both the lists are updated with the adapter.

Just one small doubt. Why are you setting a for loop inside a contructor. What do you plan to achieve?

MadScientist
  • 2,134
  • 14
  • 27
  • Hi thanks for the answer. The for loop is there because ive been experimenting to find out whats wrong. It is supposed to set quantities to the exact length as the items list. I am accessing quantities in the adapter by having it declared in the fragment class. I will try your solution of creating both lists first and then passing them to the adapter and get back to you with the result, although i think ive tried that before. the reason for this is because when i highlight an item in viewholder all corresponding items with that view are highlight so im trying to track that with these lists – ah4444 Mar 06 '17 at 00:52
  • It other view are lit. Then that has nothing to do with your lists. Check [this](http://stackoverflow.com/questions/34552070/dynamically-adding-views-in-a-recyclerview-only-to-current-item/42482524#42482524) stack post! – MadScientist Mar 06 '17 at 03:48
  • Hi thanks for your help. I got the lists into the adapter by creating them in the same place and passing them in. as for the other views, i checked the link out, but ended up continuing with this way. The lists purpose was to keep track of what has been selected so that when i scroll up and down the selection persists. wasnt sure if onViewRecycled would persist – ah4444 Mar 08 '17 at 17:22