CheckBox and setOnItemClickListener not working in android

强颜欢笑 提交于 2019-12-20 06:36:26

问题


I am using ListView with 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 it's not working.

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 Selection.

Can you please help me?


回答1:


You can get the instance of CheckBox inside onItemClick() by using setTag() and getTag(). You can setTag the CheckBox instance inside your getView() method as

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

And get the instance inside onItemClick() using,

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

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




回答2:


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() will get called




回答3:


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.




回答4:


Focusable view in the list item prevents the firing of onListItemClick() in the ListActivity when the list item is clicked. But the effect of onListItemClick() can be achieved with OnClickListener. Read here more about this



来源:https://stackoverflow.com/questions/11737294/checkbox-and-setonitemclicklistener-not-working-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!