0

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.

EDIT 1:

This is how currently my screen looks either with radiobutton or checkbox.

Updated Code to show radiobuttons screenshot

EDIT 2: I changed my code as follows which lets me to check one radiobutton at a time. The thing is that when you click on the last item in the recycler view list , the radiobutton does not get checked and if it does then when you scroll up , it vanishes.

   @Override
    public void onBindItemViewHolder(final 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.variationItemPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("I WAS CLICKED","ITEM PRICE WAS CLICKED");
            }
        });

        iHolder.radioButton.setChecked(list.get(position).isSelected());
        iHolder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("I was pressed","BUtton");




            RadioButton rb = (RadioButton) view;

            if(rb.isChecked()){
                Log.i("CONTAINS",Boolean.toString(radioButtonList.contains(rb)));
                if(position >= radioButtonList.size()){
                    radioButtonList.add(rb);
                    rb.setChecked(true);
                    list.get(position).setSelected(true);
                }else{


                }

                //list.get(position).setSelected(true);

            }

            for (int i = 0; i < radioButtonList.size(); i++) {
                RadioButton nrb = radioButtonList.get(i);
                if(i != position){
                    if(nrb.isChecked()){
                        nrb.setChecked(false);
                        list.get(i).setSelected(false);
                    }
                }
            }

            Log.i("RADIO LIST",Integer.toString(radioButtonList.size()));
            for (int i = 0; i < radioButtonList.size(); i++) {
                Log.i("RADIO CHECKED",Boolean.toString(radioButtonList.get(i).isChecked()));
            }
            Log.i("CURRENT POST",Integer.toString(position));

            for (int i = 0; i < list.size() ; i++) {
                Log.i("POST",Boolean.toString(list.get(i).isSelected()));
            }
        }
    });


    }

My Activity Class:

public class RestaurauntMenuItemDetailActivity extends AppCompatActivity implements RestaurauntMenuItemDetailInterfaceHandler{

    private static List<RestaurauntMenuItemDetail> restaurauntMenuItemDetailList = new LinkedList<>();
    RestaurauntMenuItemDetailFetchAPI restaurauntMenuItemDetailFetchAPI;
    String restaurauntSlug;
    String  menuSlug;
    int  menuId;

    int variationCount;
    List<String> variationNamesList;


    private RecyclerView sectionHeader;
    private SectionedRecyclerViewAdapter sectionAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restauraunt_menu_item_detail);
//
//        sectionAdapter = new SectionedRecyclerViewAdapter();





        restaurauntMenuItemDetailFetchAPI = new RestaurauntMenuItemDetailFetchAPI(this,this);
        restaurauntSlug =  getIntent().getExtras().getString("restaurauntSlug");
        menuSlug  = getIntent().getExtras().getString("menuSlug");
        menuId  = getIntent().getExtras().getInt("menuId");
        restaurauntMenuItemDetailFetchAPI.prepareURL(restaurauntSlug,menuSlug,menuId);
        restaurauntMenuItemDetailFetchAPI.fetchMenuItemDetail();

    }




    @Override
    public void restaurauntMenuItemDetailListDownloadTaskCompleted(List<RestaurauntMenuItemDetail> result,int variationCount,List<String> variationNames) {
        restaurauntMenuItemDetailList = result;
        this.variationCount = variationCount;
        variationNamesList = variationNames;
        sectionHeader = (RecyclerView)findViewById(R.id.menuItemDetailRecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RestaurauntMenuItemDetailActivity.this);
        sectionHeader.setLayoutManager(linearLayoutManager);
        sectionHeader.setHasFixedSize(true);


//      HeaderRecyclerViewSection secondSection = new HeaderRecyclerViewSection("Second Section", restaurauntMenuItemDetailList);
//      HeaderRecyclerViewSection thirdSection = new HeaderRecyclerViewSection("Third Section", restaurauntMenuItemDetailList);
        sectionAdapter = new SectionedRecyclerViewAdapter();
        for(int i=0; i < variationNamesList.size(); i++){

            String variationName = variationNames.get(i);
            addSectionToAdapter(variationName,sectionAdapter);
            sectionHeader.setAdapter(sectionAdapter);
        }

//        sectionAdapter.addSection(secondSection);
//        sectionAdapter.addSection(thirdSection);


//        initRecyclerView();



    }


    private void addSectionToAdapter(String variationName, SectionedRecyclerViewAdapter sectionAdapter){
        List<RestaurauntMenuItemDetail> l1 = new ArrayList<>();

        for (int i = 0; i < restaurauntMenuItemDetailList.size() ; i++) {
            if(restaurauntMenuItemDetailList.get(i).getVariationCategory().equals(variationName)){
                    l1.add(restaurauntMenuItemDetailList.get(i));
            }
        }

        HeaderRecyclerViewSection firstSection = new HeaderRecyclerViewSection(variationName, l1,sectionAdapter);
        sectionAdapter.addSection(firstSection);


    }
}
indexOutOfBounds
  • 541
  • 7
  • 27
  • Well yes classic territory for [radio button](https://developer.android.com/guide/topics/ui/controls/radiobutton) **"Radio buttons allow the user to select one option from a set."**. You can make check boxes do that, please don't (confusion will reign). – Jon Goodwin Feb 09 '19 at 21:47
  • @JonGoodwin I have updated the question. Have a look at the screenshot. You can imagine the same code with the radiobutton. I tried that before checkbox.I have a single row with a radiobutton or a checkbox, item name and price. I can select all of them by default which is i do not want. – indexOutOfBounds Feb 09 '19 at 21:51
  • Just check this answer, it may help you. https://stackoverflow.com/a/32428115/3669559 – Oğuzhan Döngül Feb 10 '19 at 09:34

2 Answers2

1

Try this. It worked for me. Might work for you as well. Just replace your OnCheckedChangeListener code with this one:

iHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton compoundButton,boolean b){
        if(compoundButton.isPressed()){
            // click is performed
            for(RestaurauntMenuItemDetail item:list){
            item.setSelected(false);
            }
            list.get(holder.getAdapterPosition()).setSelected(b);
            notifyDataSetChanged();
        }
    }
});
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • Why can I not call notifyDataSetChanged() method? It does not resolve to anything? – indexOutOfBounds Feb 10 '19 at 06:40
  • Well `notifyDataSetChanged()` refreshes the recyclerview causing each view to rebind. you can avoid this if you're using `dataBinding` – Birju Vachhani Feb 10 '19 at 09:00
  • The code you provided did not work for me. Can you have a look at the updated code. I also provided code of my activity. I am creating sections dynamically so is it also right or i am doing something wrong – indexOutOfBounds Feb 10 '19 at 09:28
0

I looked at a lot of other questions posted by users and got a little help by the comments provided here. What i actually wanted was to able to check one radio box at a time and secondly, when we scroll down in our recycler view either check box from the radio button vanished or it was wrong.

We need to keep a boolean variable in our class which holds our object and we can set it to false by default. Generate the getters and setters. The code is as follows:

The list goes at the top, just in case.

 List<RadioButton> radioButtonList = new ArrayList<>();
    private int selectedPosition = -1;


   @Override
    public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ItemViewHolder iHolder = (ItemViewHolder)holder;
        final RestaurauntMenuItemDetail r = list.get(position);



        iHolder.radioButton.setTag(position);

        iHolder.radioButton.setOnCheckedChangeListener(null);
        iHolder.radioButton.setChecked(r.getSelected());


        iHolder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Integer pos = (Integer) iHolder.radioButton.getTag();


                RadioButton rb = (RadioButton) v;



                if(radioButtonList.isEmpty()){
                    radioButtonList.add(rb);
                    rb.setChecked(true);
                    list.get(pos).setSelected(true);
                    selectedPosition = pos;
                }else{
                    RadioButton nrb = radioButtonList.get(0);
                    if(nrb.isChecked()){
                        Log.i("NEW RB BUTTON WAS CHECKED", "CHECKED NRB");
                        nrb.setChecked(false);
                        list.get(selectedPosition).setSelected(false);
                    }
                    radioButtonList.remove(0);
                    radioButtonList.add(rb);
                    rb.setChecked(true);
                    list.get(pos).setSelected(true);
                    selectedPosition = pos;

                }
            }
        });
indexOutOfBounds
  • 541
  • 7
  • 27