vendredi 21 août 2015

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"/> 

Aucun commentaire:

Enregistrer un commentaire