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

时间:2014-12-31 17:28:37

标签: java android listview adapter

我目前正在使用带有集成Checkbox的View的ListBox。 我的问题是我的Checkbox不再改变,因为我添加了一个 onCheckedChange 监听器,我将项目的状态保存到上面的列表框中。它始终标记为unchecked,我的监听器始终将true作为参数isChecked的值。

我的布局:

<?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>

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;
    }
}

最后但并非最不重要..我的输出。如您所见,复选框的状态永远不会设置为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

也许有一个简单的解决方案,但目前我只能弄清楚发生了什么。

谢谢!

修改:问题是行((ListView)parent).setItemChecked(position, isChecked);。它调用方法rememberSyncState()requestLayout()。猜猜是问题......有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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;
    }
}