samedi 9 février 2019

Unable to allow user to check one checkbox at a time

I have a sectioned recyclerview implemented which works great. I want to include a checkbox in all my items which show up under a section.The thing I want to implement now is to allow user to check one checkbox at a time. I have tried radiobutton also but the problem stays the same as I am unable to get hold of the listeners.

I have searched for it on stackoverflow but I could not get it work. getTag() gives me a null pointer exception and I have also tried keeping a selectedPosition variable and trying but I could not get the getTag() to work.

public class HeaderRecyclerViewSection extends StatelessSection {
    private static final String TAG = HeaderRecyclerViewSection.class.getSimpleName();
    private String title;
    private List<RestaurauntMenuItemDetail> list;

    private List<RestaurauntMenuItemDetail> checkedList;

    private int selectedPosition = -1;


    public HeaderRecyclerViewSection(String title, List<RestaurauntMenuItemDetail> list) {
        super(R.layout.section_ex1_header, R.layout.layout_section_item);
        this.title = title;
        this.list = list;
    }
    @Override
    public int getContentItemsTotal() {
        return list.size();
    }
    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {

        return new ItemViewHolder(view);
    }


    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ItemViewHolder iHolder = (ItemViewHolder)holder;
        iHolder.variationItemName.setText(list.get(position).getVariationItemName());
        iHolder.variationItemPrice.setText(Double.toString(list.get(position).getVariationItemPrice()));
        iHolder.checkBox.setOnCheckedChangeListener(null);
        iHolder.checkBox.setChecked(list.get(position).isSelected());

        Log.i("EXECUTED AGAIN","AGAIN");
        iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(selectedPosition == position){

                    list.get(selectedPosition).setSelected(false);
                    iHolder.checkBox.setChecked(list.get(selectedPosition).isSelected());
                }
                list.get(position).setSelected(b);
                Log.i("POSITION is",Integer.toString(position));
                Log.i("B Is",Boolean.toString(b));
                selectedPosition = position;



         }
     });
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeaderViewHolder(view);
    }
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        HeaderViewHolder hHolder = (HeaderViewHolder)holder;
        hHolder.headerTitle.setText(title);
    }
    }

My ItemHolder class:

public class ItemViewHolder extends RecyclerView.ViewHolder{
    public TextView variationItemName;
    public TextView variationItemPrice;
    CheckBox checkBox;

    public ItemViewHolder(View itemView) {
        super(itemView);
        variationItemName = (TextView)  itemView.findViewById(R.id.variationItemName);
        variationItemPrice = (TextView) itemView.findViewById(R.id.variationItemPrice);
        checkBox = itemView.findViewById(R.id.checkBox2);
    }
}

What i am trying to achieve is to allow user to be able to select one checkbox under a certain section.




Aucun commentaire:

Enregistrer un commentaire