-2

I am using sectional recyclerview for add to cart screen. and want to select only one checkbox at a time from various items from category. Below is code and screen image. Please give me solution for select only one checkbox at a time from product categories.enter image description here

Below is json response,

{
 "Status":"Success",
 "StatusCode":"200",
 "Message":"data fetch successfully.",
 "Data":{
  "1":{
     "OptionGroupName":"Base",
     "OptionGroupId":"1",
     "OptionCount":2,
     "Options":[
        {
           "OptionGroupId":"1",
           "OptionGroupName":"Base",
           "ProductId":"54",
           "OptionId":"1",
           "OptionName":"Soft",
           "OptionPrice":"25",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"1",
           "OptionGroupName":"Base",
           "ProductId":"54",
           "OptionId":"2",
           "OptionName":"Hard",
           "OptionPrice":"15",
           "IsActive":"1"
        }
     ]
  },
  "2":{
     "OptionGroupName":"Sauce",
     "OptionGroupId":"2",
     "OptionCount":3,
     "Options":[
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"3",
           "OptionName":"Shezwan",
           "OptionPrice":"10",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"4",
           "OptionName":"Chilly",
           "OptionPrice":"20",
           "IsActive":"1"
        },
        {
           "OptionGroupId":"2",
           "OptionGroupName":"Sauce",
           "ProductId":"54",
           "OptionId":"5",
           "OptionName":"Soya",
           "OptionPrice":"29",
           "IsActive":"1"
        }
     ]
  }
  }
  }

This is java code;

  public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
    private List<ProductCart> allData;

    public ProductCartAdapter(List<ProductCart> data) {
        this.allData = data;
    }

    @Override
    public int getSectionCount() {
        return allData.size();
    }

    @Override
    public int getItemCount(int section) {
        return allData.get(section).getItemList().size();
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
        String sectionName = allData.get(section).getName();
        SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
        sectionViewHolder.tvProductName.setText(sectionName);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
        final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
        itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());

        **// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**

        itemViewHolder.cbProduct.setTag(section);
        itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Integer pos = (Integer) itemViewHolder.cbProduct.getTag();

                if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
                    allData.get(pos).getItemList().get(relativePosition).setSelected(false);
                } else {
                    allData.get(pos).getItemList().get(relativePosition).setSelected(true);
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
        View v = null;
        if (header) {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_section_cart, parent, false);
            return new SectionViewHolder(v);
        } else {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_cart_item, parent, false);
            return new ItemViewHolder(v);
        }

    }

    // SectionViewHolder Class for Sections
    public class SectionViewHolder extends RecyclerView.ViewHolder {
        final TextView tvProductName;

        public SectionViewHolder(View itemView) {
            super(itemView);
            tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
        }
    }

    // ItemViewHolder Class for Items in each Section
    public class ItemViewHolder extends RecyclerView.ViewHolder {

        final TextView tvProductName, tvProductPrice;
        CheckBox cbProduct;

        public ItemViewHolder(final View itemView) {
            super(itemView);
            tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
            tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
            cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
        }
    }

}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57

2 Answers2

1
      public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
        private List<ProductCart> allData;

        public ProductCartAdapter(List<ProductCart> data) {
            this.allData = data;
        }

        @Override
        public int getSectionCount() {
            return allData.size();
        }

        @Override
        public int getItemCount(int section) {
            return allData.get(section).getItemList().size();
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
            String sectionName = allData.get(section).getName();
            SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
            sectionViewHolder.tvProductName.setText(sectionName);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
            itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());

            **// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**

            itemViewHolder.cbProduct.setTag(section);
            itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetCategoryWiseData(section);
                    Integer pos = (Integer) itemViewHolder.cbProduct.getTag();

                    if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(false);
                    } else {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(true);
                    }
    notifyItemChanged(pos);
                }
            });

    *********************************************************

        //code for checked/unchecked
        if(allData.get(section).getItemList().get(relativePosition).getSelected()){
        itemViewHolder.cbProduct.setChecked(true);
        }else{
        itemViewHolder.cbProduct.setChecked(false);
        }

    **************************************************************
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
            View v = null;
            if (header) {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_section_cart, parent, false);
                return new SectionViewHolder(v);
            } else {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_cart_item, parent, false);
                return new ItemViewHolder(v);
            }

        }

        // SectionViewHolder Class for Sections
        public class SectionViewHolder extends RecyclerView.ViewHolder {
            final TextView tvProductName;

            public SectionViewHolder(View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
            }
        }

        // ItemViewHolder Class for Items in each Section
        public class ItemViewHolder extends RecyclerView.ViewHolder {

            final TextView tvProductName, tvProductPrice;
            CheckBox cbProduct;

            public ItemViewHolder(final View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
                tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
                cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
            }
        }

private void  resetCategoryWiseData(int section){
    for(int i=0;i<allData.get(section).getItemList().size();i++){
         allData.get(section).getItemList().get(i).setSelected(false);
     }
  }

add this line  **notifyItemChanged(pos);** in click listener of checkox.

and add some code that i have already written in above for ( checkbox is checked or not )


if any issue then tell me.Happy Coding.:)
dipali
  • 10,966
  • 5
  • 25
  • 51
0

If you want to check only one item then use RadioGroup instead or recyclerview(inside one).

So that user will know that he/she can select only one item from the list. This will improve user understanding also it will reduce looping again for checking any item has been checked or not.

Vignesh KM
  • 1,979
  • 1
  • 18
  • 24