S.P.
S.P.

Reputation: 2564

How can I select only one Checkbox in a dynamic view

I have a screen that add dynamically a row with two textView and one CheckBox. My problem is that I only want that the user could check one, and the others have to be uncheck. If the user push another checkbox, enable this and disable all the other checkbox. I've test some things but I don't get the functionality that I want.

I can't change the configuration from checkbox to radioButton and I have to use the method that I'm using:

private selected_position = -1;

protected void addGreetingToListView(final GreetingListJson greetings) {
        View row = inflater.inflate(R.layout.main_greetings_settings_row, greetingListView, false);
        row.setTag(greetings);

        playButton = (ToggleButton) row.findViewById(R.id.detail_play_greeting);            
        TextView greetingName = (TextView)row.findViewById(R.id.greetingTitle);
        greetingName.setDuplicateParentStateEnabled(true);
        TextView greetingDescription = (TextView)row.findViewById(R.id.greetingDescription);
        greetingDescription.setDuplicateParentStateEnabled(true);

        checkBox = (CheckBox) row.findViewById(R.id.checkBox_greeting_list);
        checkBox.setTag(greetings);


        checkBox.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick (View view) {

                if (((CheckBox) view).isChecked())
                {
                    selected_position= greetingListView.getChildCount();
                }
                else
                {
                    selected_position=-1;
                }           
            }

        });


                greetingName.setText(greetings.Name);
                greetingDescription.setText(greetings.Description);


            row.setOnClickListener(this);
            row.setOnLongClickListener(this);
            row.setFocusable(true);


            greetingListView.addView(row);

    }

The XML of the Checkbox:

   <CheckBox
         android:id="@+id/checkBox_greeting_list"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_marginRight="15dp"
         android:layout_centerVertical="true"/> 

Upvotes: 1

Views: 7543

Answers (1)

shhp
shhp

Reputation: 3703

Firstly, declare an ArrayList in the class:

ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();

Then in addGreetingToListView add every new checkbox to mCheckBoxes and modify the click listener of the checkbox:

checkBox.setTag(greetings);

mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick (View view) {

            if (((CheckBox) view).isChecked())
            {
                for (int i = 0; i < mCheckBoxes.size(); i++) {
                     if (mCheckBoxes.get(i) == view)
                         selected_position = i;
                     else 
                         mCheckBoxes.get(i).setChecked(false);
                }

            }
            else
            {
                selected_position=-1;
            }           
        }

    });

Upvotes: 4

Related Questions