CheckBox y setOnItemClickListener no funcionan en android

Estoy utilizando ListView con CheckBox. I have selected only one CheckBox but it's automatically selected into Multiple Rows using ScrollView.

Sample Code (This Code is working Perfectly)

       holder.checkbox.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                CheckBox cb = ( CheckBox ) v;
                if ( cb.isChecked() )
                {
                    test[position] = true;
                    holder.checkbox.setChecked(test[position]);
                }
                else
                {
                    test[position] = false;
                    holder.checkbox.setChecked(test[position]);
                }
            }
        });
        holder.checkbox.setChecked(test[position]);

But When i uses setOnItemClickListener no funciona.

Sample Code (This code is not working for me)

        listview.setOnItemClickListener( new OnItemClickListener() 
        {
            public void onItemClick( AdapterView<?> parent, final View v, final int position, long id ) 
            {
                holder.checkbox = ( CheckBox ) v.findViewById( R.id.lock_File_CheckBox );
                if ( holder.checkbox.isChecked() )
                {
                    test[position] = false;
                    holder.checkbox.setChecked(test[position]);
                }
                else
                {
                    test[position] = true;
                    holder.checkbox.setChecked(test[position]);
                }
            }
        });
        holder.checkbox.setChecked(test[position]);

Where i can mistaken. How do avoid this CheckBox Selección

¿Podrías ayudarme?

preguntado el 31 de julio de 12 a las 10:07

What is the problem?? Do you want to check more then 1 check-box? -

I have selected One Row but it's point out multiple Row. -

Pardon, I am not getting you.! -

Refiera este enlace stackoverflow.com/questions/6846151/problem-with-checkbox I have a same Problem. When i am using listview.setOnItemClickListener it's not working perfectly. That is Now i can First and Second Row (Check Box is Checked) in the same time Last Two Row (CheckBox is Checked). How do solve this. -

Cuando estoy usando listview.setOnItemClickListener to select the CheckBox, In the problem is that when I select one checkbox, if I scroll the list there are and other checkbox selected(the distance between them is 10) and I don't understand why. Can anyone help me? -

4 Respuestas

To make your listview focusable, remove focus from the items themselves. Add the following after instantiating listview:

listview.setItemsCanFocus(false);

Then add the following to your list_item.xml

<CheckBox
    android:id="@+id/lock_File_CheckBox"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

With this, your setOnItemClickListener() será llamado

Respondido el 01 de Septiembre de 14 a las 15:09

You can get the instance of CheckBox inside onItemClick() mediante el uso setTag() y getTag(). You can setTag the CheckBox instance inside your getView() método como

convertView.setTag(R.id.check, viewHolder.checkbox);

And get the instance inside onItemClick() utilizando,

CheckBox checkbox = (CheckBox) v.getTag(R.id.check);

If you have any further query you can check my blog post.

Respondido 31 Jul 12, 10:07

When i am using listview.setOnItemClickListener to select the CheckBox, In the problem is that when I select one checkbox, if I scroll the list there are and other checkbox selected(the distance between them is 10) and I don't understand why. - Sekar

Its happening because of recycling mechanism. - Lalit Poptani

Checkboxes Randomly Checked/Unchecked in Expandable List, How do avoid recycling mechanism. Can you help me. - Sekar

Well, I had already given you my example link, you can download and check how I managed using setTag() y getTag() by saving the position of ListItem(CheckBox). - Lalit Poptani

Ok. Thanks for information. Now I can work out, after i inform the result. Thank you. - Sekar

Don't use onClick. Use OnCheckedChange

holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked) {
                doSomethingWithCheckedState(compoundButton);
            } else {
                doSomethingWithUnCheckedState(compoundButton);
            }
        }
    });

Keep in mind that CheckBox inherit from CompoundButton for ICS's switch compatibility.

Respondido 31 Jul 12, 10:07

Dam holder.checkbox.setOnClickListener it's working. But listview.setOnItemClickListener no funciona. - Sekar

Right. So the problem is not coming from you sample code, but from your adapter. If there is multiple CheckBox being selected, it must be a reference problem. - Dam

When I am select a First Row CheckBox can be checked First and Twelth Row. When I am select a Second Row CheckBox can be checked Second and Thirteen Row. Why it's happened. - Sekar

Focusable view in the list item prevents the firing of onListItemClick() en la ListActivity when the list item is clicked. But the effect of onListItemClick() se puede lograr con OnClickListener. Leer aquí más sobre esto

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.