1

I'm using SectionedRecyclerViewAdapter and i'm pass to myAdapter two sections

first section has 16 elements , and second section has 18 elements

each element has CheckBox Item and i'm try calling method that set all items as checked , but when looping on it and get item number 17 it returns null

but my mRecyclerView actually has 34 items as a total

so how to move to other section and get element number 17.

            sectionAdapter = new SectionedRecyclerViewAdapter();
    ArrayList<Item> contacts = new ArrayList<>();
   // ArrayList<String> dishNames = new ArrayList<>();
    mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));

    for(int d = 0 ; d<DishesIngredientsShoppingList.size() ;d++) {
        contacts =  new ArrayList<>();
        for (int i = 0; i < DishesIngredientsShoppingList.get(d).getGroups().length; i++) {
            for (int g = 0; g < DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients().length; g++) {
                contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getName()));
            }
        }
        sectionAdapter.addSection(new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts));
   //     contacts.clear();
    }


  class ContactsSection extends StatelessSection {

    String title;
    List<Item> list;
    ItemViewHolder row;
    HeaderViewHolder MainRow;
    public ContactsSection(String title, ArrayList<Item> list) {
        super(R.layout.shopping_row_item_header, R.layout.shopping_row_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // list here has 16 elements !! it should 34 elements 
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ItemViewHolder itemHolder = (ItemViewHolder) holder;
        final Item name = list.get(position);
        itemHolder.tvItem.setText(name.name);
        ((ItemViewHolder) holder).IntegBOX.setChecked(name.checked);



        chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {




                for (int i = 0; i <sectionAdapter.getItemCount(); i++) {
                    final Item name = list.get(i);
                    name.checked = true;
                    sectionAdapter.notifyDataSetChanged();
                }



            }
        });

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

        headerHolder.tvTitle.setText(title);
    }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTitle;
        private final CheckBox DishBOX;
        public HeaderViewHolder(View view) {
            super(view);

            tvTitle = (TextView) view.findViewById(R.id.DishName);
            DishBOX = (CheckBox) view.findViewById(R.id.chooseDish);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{

        private final View rootView;
        private final TextView tvItem;
        private final CheckBox IntegBOX;
        public ItemViewHolder(View view) {
            super(view);

            rootView = view;
            tvItem = (TextView) view.findViewById(R.id.DishName);
            IntegBOX = (CheckBox) view.findViewById(R.id.chooseDish);
            IntegBOX.setOnCheckedChangeListener(this);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = getAdapterPosition();
            list.get(position).checked = isChecked;
        }
    }


    }
class Item {
    String name;
    boolean checked;

    public Item(String name) {
        this.name = name;
        checked = false;
    }
}
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
Heshsm Tl4s
  • 39
  • 1
  • 7
  • 1
    read the docs: `This method checks only the children of RecyclerView. If the item at the given position is not laid out, it will not create a new one.` and `Returns The ViewHolder at position or null if there is no such item` – pskink May 14 '16 at 05:38
  • OK , so how to get item 17 , I need a soulation – Heshsm Tl4s May 14 '16 at 08:51
  • the same way you are getting any item inside `onBindViewHolder` method – pskink May 14 '16 at 08:57
  • yes this for loop are inside onBindViewHolder and returbns null when getting item 17 – Heshsm Tl4s May 14 '16 at 10:52
  • 1
    what do you need `findViewHolderForAdapterPosition` for? just change your data model and call `notifyDataSetChanged()` / `notifyItemChanged(int position)` / `notifyItemRangeChanged(int positionStart, int itemCount)` – pskink May 14 '16 at 10:56
  • as i said i need it when i call some activity button it make all items in check state so i want to get access to all elements and make all as checked state , delete , and share etc – Heshsm Tl4s May 14 '16 at 11:47
  • so when you click some button change your adapter's data and call one of the "notify" method – pskink May 14 '16 at 12:02
  • can not reslove method notifyItemChanged() , – Heshsm Tl4s May 14 '16 at 12:12
  • all of them belong to RecyclerView.Adapter class – pskink May 14 '16 at 12:14
  • i update my code with full onBinderViewHolder method , the problem getting in chooseAll button click when the for loop get item number 17 it returns null – Heshsm Tl4s May 14 '16 at 12:14
  • same error null : java.lang.NullPointerException: Attempt to read from field 'android.widget.CheckBox com..mainActivities.ShoppingListActivity$HeaderViewHolder.DishBOX' on a null object reference – Heshsm Tl4s May 14 '16 at 13:34
  • you dont need any holder at all, just make the changes in your data model and call "notify" method `Item item = list.get(position);` class `Item` will have `String name` and `booleam checked` fields to store the name and "checked" indicator – pskink May 14 '16 at 13:37
  • sorry really i can not understand you , i need to call viewHolder to every holder beacuse there was checkbox itemView in every row , so when click on chooseAll button it should make all ViewHolders are in checked or pressed state , notiy dose not help me , and list.get(position); it return string not Item class :) , please read my Q again , my problem is first section works when setting all checkbox state but when i get element number 17 (the begining of second section ) it returns null object , so i have to check every holder is this itemHolder or headerItemHolder? then make it checked – Heshsm Tl4s May 14 '16 at 13:50
  • no, you dont need to "call" any ViewHolder, how many views do you have per each item? two? text view + check box? if so, your list should also keep two fields per item, see [this](http://pastebin.com/NMAxeXVt) – pskink May 14 '16 at 15:22
  • ok nice , so now , how to implement chooseAll button to set all 34 items as in checked state ? – Heshsm Tl4s May 14 '16 at 15:43
  • i already said it twice: update your data model (`List list` in my case) and call "notify" method – pskink May 14 '16 at 15:47
  • i was did as you said changed data model and put notify put there is no change in checkBox state : for (int i = 0; i < getContentItemsTotal(); i++) { name.checked=true; sectionAdapter.notifyItemChanged(i);} – Heshsm Tl4s May 14 '16 at 16:15
  • i have called notify method but it works until element number 15 then i get error null and says invild index 16 size is 16 – Heshsm Tl4s May 14 '16 at 16:31
  • this line causes the error list.get(position).checked = isChecked; , it has 16 element of section one , so how to get all elements of section one and two to get it 34 – Heshsm Tl4s May 14 '16 at 16:41
  • check [this](http://pastebin.com/dgFYzthc) code, run it, uncheck some check boxes, then click "toggle" button, then analyze the code, see how it works and modify to fit your needs – pskink May 14 '16 at 16:45
  • same thing please note that i use two section , your code it works fine if i have only one list or one section , it works fine until it reach finel element in section one which has 16 element when it moving to element 17 of section two it get null beacuse list.get(17) return null because list of Items has only 16 element , so how to make my list Items has 34 elements? how to make it access all elements with all section – Heshsm Tl4s May 14 '16 at 16:53
  • it all depends how you structure your data, i cannot tell you anything as i dont see your structure – pskink May 14 '16 at 16:57
  • i will update my code now – Heshsm Tl4s May 14 '16 at 16:59
  • done see that , note that evey section has 16 elements in list . – Heshsm Tl4s May 14 '16 at 17:02

1 Answers1

0

Add a method in your ContactsSection class to check all items:

public void checkAllItems() {
    for (Item item : list) {
        item.checked = true;
    }
}

Then change your onClick code to:

chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        for(Section section : sectionAdapter.getSectionsMap().values()) {
            section.checkAllItems();    
        }
        sectionAdapter.notifyDataSetChanged();

    }
});
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71