-1

I'm using sectioned RecycleView library from here :

https://github.com/luizgrp/SectionedRecyclerViewAdapter

but what I try to do is when select some multi check box from section 1 and section two and hit remove button , the sectionAdapter.notifyItemRemoved(key); dose not do anything . the view or row is the same , it should be updated and remove the selected row

what is the best implementation for this case to use?

remove function code:

   private void getDishesIngredientsShoppingList() {

    appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(mActivity);

    Gson gson = new Gson();
    String json2 = appSharedPrefs.getString("DishesIngredientsShoppingList", "");
    Type type2 = new TypeToken<ArrayList<Dishes>>() {
    }.getType();
    DishesIngredientsShoppingList = gson.fromJson(json2, type2);

    // Check if ArrayList null
    if (DishesIngredientsShoppingList == null) {
        DishesIngredientsShoppingList = new ArrayList<>();
    }

    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++) {
                if (Language)
                    contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getName()));
                else
                    contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getNameEnglish()));


            }
        }


   if (Language)
            sectionAdapter.addSection(DishesIngredientsShoppingList.get(d).getName(), new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts));
        else
            sectionAdapter.addSection(DishesIngredientsShoppingList.get(d).getEnglishName(), new ContactsSection(DishesIngredientsShoppingList.get(d).getEnglishName(),contacts));


    }
    mRecyclerView.setAdapter(sectionAdapter);

}


    class ContactsSection extends StatelessSection {

    final String TAG;
    String title;
    List<Item> list;
    ItemViewHolder row;
    HeaderViewHolder MainRow;

    ContactsSection(String title, ArrayList<Item> list) {
        super(R.layout.shopping_row_item_header, R.layout.shopping_row_item);
        this.title = title;
        this.list = list;
        this.TAG = title;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size();
    }

    @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);
        ((ItemViewHolder) holder).IntegBOX.setTag(position);


        deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                removeAtAll();
            }
        });


        itemHolder.IntegBOX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Set the button's appearance
                ((CheckBox) v).setChecked(!((CheckBox) v).isChecked());

                // if button Already in checked statue
                if (((CheckBox) v).isChecked()) {

                    Iterator it = selectedCheckBox.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry pair = (Map.Entry) it.next();
                        int key = (Integer) pair.getKey();
                        if (key == (Integer) v.getTag()) {
                            it.remove();
                        }
                        // set it as false
                        ((CheckBox) v).setChecked(false);
                    }
                } else {
                    // Otherwise set it to true and add it to hashMap
                    ((CheckBox) v).setChecked(true);
                    selectedCheckBox.put((Integer) v.getTag(), true);
                }


                Toast.makeText(mActivity, String.format("Clicked on position #%s of Section %s", sectionAdapter.getSectionPosition(itemHolder.getAdapterPosition()), title), Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    // Remove All Btn Fuc
    void removeAtAll() {
        Iterator it = selectedCheckBox.entrySet().iterator();
        if (!it.hasNext()) {

            new makeDialog().makeDialog(mActivity, mActivity.getResources().getString(R.string.share_no_data));

        } else {

            //     int hesham = sectionAdapter.getSectionForPosition(0).getContentItemsTotal();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                int key = (Integer) pair.getKey();
                if ((Boolean) pair.getValue()) {


 int contentItemsTotal = getContentItemsTotal();
 sectionAdapter.notifyItemRangeRemovedFromSection(TAG, 0, contentItemsTotal);




                    //  int positionInSection = sectionAdapter.getPositionInSection(key);

                    //   list.remove(positionInSection);
                   // sectionAdapter.notifyItemRemovedFromSection(TAG, 2);
                    //   DishToDel.add(dish.get(key).getDishID());
                    // sectionAdapter.getSectionForPosition(0).getContentItemsTotal();
                    //    list.remove(key);
                    //  it.remove();
                 /*   hesham--;*/

                    //  sectionAdapter.notifyItemRemoved(key);

                }
                // sectionAdapter.notifyItemRangeChanged(key, hesham);
            }
            chooseAllradioButton.setSelected(false);
        }
    }
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71

1 Answers1

0

The parameter for notifyItemRemoved is the position of the item in the whole list of items of the RecyclerView, which takes into account the headers/footers of your sections if they have them.

So let's say your RecyclerView looks like this:

Pos | Item
-----------------------
0   | Section 1 Header
1   | Section 1 Item 0
2   | Section 1 Item 1
3   | Section 2 Header
4   | Section 2 Item 0
5   | Section 2 Item 1

And your Map has all the items of all the Sections:

Pos | Item
-----------------------
0   | Section 1 Item 0
1   | Section 1 Item 1
2   | Section 2 Item 0
3   | Section 2 Item 1

When the item from position 0 in your map gets removed ("Section 1 Item 0"), you are calling notifyItemRemoved for position 0, which is the "Section 1 Header" in the RecyclerView. But "Section 1 Header" wasn't removed from the RecyclerView, that's why notifyItemRemoved has no effect.

I would suggest you to have a look on how notifyItemRangeRemovedFromSection is used in example 8 and try to do something similar.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • notifyItemRemoved it works , but how to update the layout , after notifyItemRemoved the layout order is get in strange behavior somthing get's up and down for different positions , – Hesham El-Nemr Apr 19 '17 at 20:55
  • I try to use sectionAdapter.notifyItemRangeRemovedFromSection(TAG, 0, contentItemsTotal); , but it return error : java.lang.IllegalArgumentException: Invalid tag: Stuffed Shrimp – Hesham El-Nemr Apr 19 '17 at 20:57
  • you have to add the section to the adapter with that "TAG", check it out in [example 8](https://github.com/luizgrp/SectionedRecyclerViewAdapter/blob/22e73815e88b47e8a1e9b5890f191d6e5075c07a/app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/Example8Fragment.java#L87) – Gustavo Pagani Apr 19 '17 at 21:04
  • yes i did that with : sectionAdapter.addSection(new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts)); – Hesham El-Nemr Apr 19 '17 at 21:07
  • so you have to pass the same DishesIngredientsShoppingList.get(d).getName‌​() to notifyItemRangeRemovedFromSection – Gustavo Pagani Apr 19 '17 at 21:09
  • yes i did it and debug code this two are same string , but it return error – Hesham El-Nemr Apr 19 '17 at 21:13
  • the code you posted above is not adding the section with a tag: `sectionAdapter.addSection(new ContactsSection(DishesIngredientsShoppingList.get(d).getName‌​(),contacts)); `. You have to pass the tag as the first parameter to `addSection`: `sectionAdapter.addSection(DishesIngredientsShoppingList.get(d).getName‌​(), new ContactsSection(DishesIngredientsShoppingList.get(d).getName‌​(),contacts)); ` – Gustavo Pagani Apr 19 '17 at 21:18
  • ok it works now , but there is no effect , getContentItemsTotal it return only 9 element , but i have in total with two lists i have 20 element , so when i select some from section 1 and some of section 2 , how code will know where i'm current section ? – Hesham El-Nemr Apr 19 '17 at 21:29
  • this [code](https://github.com/luizgrp/SectionedRecyclerViewAdapter/blob/22e73815e88b47e8a1e9b5890f191d6e5075c07a/app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/Example1Fragment.java#L106) from the example knows where it is in the current section – Gustavo Pagani Apr 19 '17 at 21:52
  • ok , but now i'm uses : int contentItemsTotal = getContentItemsTotal(); sectionAdapter.notifyItemRangeRemovedFromSection(TAG, 0, contentItemsTotal); but no effect is taken , there is no row removed or deleted – Hesham El-Nemr Apr 19 '17 at 22:05
  • It seems that you have are having many different problems with your project but you should ask [one question per post](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) and also do some [research](https://stackoverflow.com/help/how-to-ask). – Gustavo Pagani May 01 '17 at 21:23
  • here is http://stackoverflow.com/questions/43726876/wrong-section-tag-value thanks – Hesham El-Nemr May 01 '17 at 21:34