lundi 4 novembre 2019

Only two checkbox can select user , how to achieve this?

I want only select two checkboxes from the listing of checkboxes..................................................................................................................................................................................................................................................................................

Below is my code.................................................;

 public class OfferItemsAdapter extends RecyclerView.Adapter<OfferItemsAdapter.ViewHolder> {
    private ArrayList<TaskListModel.Option> mDataset;
    String strFreeItem;

    OfferItemsAdapter(ArrayList<TaskListModel.Option> reviwsLists, String strFreeItem) {
        mDataset = reviwsLists;
        this.strFreeItem = strFreeItem;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.addon_list_cart_item, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.cbProduct.setText(mDataset.get(position).getItemName());
        holder.tvProductPrice.setVisibility(View.GONE);

        if (strFreeItem.equalsIgnoreCase("1")) {

            if (mDataset.get(position).isChecked()) {
                holder.cbProduct.setChecked(true);
                selectedItems.add(mDataset.get(position).getItemID());
            } else {
                holder.cbProduct.setChecked(false);
                selectedItems.remove(mDataset.get(position).getItemID());
            }

        }

        holder.cbProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (strFreeItem.equalsIgnoreCase("1")) {

                    if (selectedItems.size() == 1 && selectedItems.contains(mDataset.get(position).getItemID())) {
                        mDataset.get(position).setChecked(false);
                    } else {
                        mDataset.get(position).setChecked(true);
                    }
                }

                notifyDataSetChanged();

                Log.e("selectedItems.toString()............",""+selectedItems.toString());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataset == null ? 0 : mDataset.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public View mainView;
        TextView tvProductPrice;
        CheckBox cbProduct;

        public ViewHolder(View v) {
            super(v);
            mainView = v;
            tvProductPrice = v.findViewById(R.id.tvProductPrice);
            cbProduct = v.findViewById(R.id.cbProduct);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire