有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ListView中的java复选框工作不正常

我目前正在处理一个带有集成复选框的视图的列表框。 我的问题是,自从我添加了一个onCheckedChange监听器,在该监听器中我将项目的状态保存到上面的列表框中后,我的复选框不再更改。它总是标记为unchecked,我的侦听器总是将true作为参数isChecked的值

我的布局:

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

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

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

    <TextView
        ...
        安卓: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) 个答案

  1. # 1 楼答案

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