简体   繁体   English

ListView中的复选框无法正常工作

[英]Checkbox in ListView not working properly

I am currently working on a ListBox with a an View with integrated Checkbox. 我目前正在使用带有集成复选框的视图的ListBox。 My problem is that my Checkbox is not changing anymore since I added a onCheckedChange Listener where I save the state of the item to the upper listbox. 我的问题是,自从我添加了onCheckedChange Listener之后,我的Checkbox不再更改,我将项目的状态保存到了上方的列表框中。 It is always marked as unchecked and my Listener always gets true as value for the parameter isChecked . 始终将其标记为unchecked并且由于参数isChecked值,我的侦听器始终为true

My Layout: 我的版式:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <CheckBox
        ...
        android:id="@+id/checkBox"/>

    <TextView
        ...
        android:id="@+id/textNumber"/>

    <TextView
        ...
        android:id="@+id/textName"/>
</RelativeLayout>

And the CustomArrayAdapter : 还有CustomArrayAdapter

public class ContactArrayAdapter extends ArrayAdapter<Contact> {

    private List<Contact> _contacts;

    public ContactArrayAdapter(Context context, List<Contact> contacts) {
        super(context, R.layout.contact_item, contacts);

        _contacts = contacts;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder holder;
        if ( convertView == null ) {
            LayoutInflater inflater = (LayoutInflater.from(this.getContext()));

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.contact_item, parent, false);
            // Some other stuff ...

            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    ((ListView)parent).setItemChecked(position, isChecked);
                    System.out.println(isChecked);
                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        return convertView;
    }

    private static class ViewHolder {
        public CheckBox checkBox;
        public TextView textName;
        public TextView textNumber;
    }
}

And last but not least.. My output. 最后但并非最不重要..我的输出。 As you can see the state of the checkbox is never set to checked and the checkbox remains trying to set to true . 如您所见,复选框的状态永远不会设置为checked ,而复选框仍会尝试设置为true

12-31 18:13:21.281  I/System.out﹕ true
12-31 18:13:21.513  I/System.out﹕ true
12-31 18:13:21.698  I/System.out﹕ true
12-31 18:13:21.850  I/System.out﹕ true
12-31 18:13:21.966  I/System.out﹕ true

Maybe there is an easy solution, but at the moment I just can figure out whats happening. 也许有一个简单的解决方案,但是目前我只能弄清楚发生了什么。

Thank you! 谢谢!

Edit : The problem is the line ((ListView)parent).setItemChecked(position, isChecked); 编辑 :问题是行((ListView)parent).setItemChecked(position, isChecked); . It calls the methods rememberSyncState() and requestLayout() . 它调用方法rememberSyncState()requestLayout() Guess that is the problem... Any ideas? 猜猜这是问题所在...有什么想法吗?

public class ContactArrayAdapter extends ArrayAdapter<Contact> {

    private List<Contact> _contacts;

    public ContactArrayAdapter(Context context, List<Contact> contacts) {
        super(context, R.layout.contact_item, contacts);

        _contacts = contacts;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if ( convertView == null ) {
            LayoutInflater inflater = (LayoutInflater.from(this.getContext()));

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.contact_item, parent, false);
            // Some other stuff ...

            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(new CheckchangeListener(parent,position) );

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        return convertView;
    }
class CheckchangeListener implements OnCheckedChangeListener {
        private ViewGroupparent;

        public CheckchangeListener(ViewGroup  parent,int position) {
            // TODO Auto-generated constructor stub

            this.parent= parent;
this.position= position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

             ((ListView)parent).setItemChecked(position, isChecked);
                    System.out.println(isChecked);
        }
    }

    private static class ViewHolder {
        public CheckBox checkBox;
        public TextView textName;
        public TextView textNumber;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM