android – Force Listview不重用视图(Checkbox)

我为Listview中的每个项目创建了一个自定义Listview(不覆盖getView()方法),具有以下布局

contactlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:weightSum="1">
    <CheckBox android:id="@+id/checkBox1" android:text="CheckBox" android:layout_width="134dp" android:layout_height="108dp" android:focusable="false"></CheckBox>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="87dp" android:layout_weight="0.84" android:weightSum="1" >
        <TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/name" android:layout_weight="0.03"></TextView>
        <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/phone"></TextView>
        <TextView android:text="TextView" android:layout_height="wrap_content" android:layout_weight="0.03" android:layout_width="match_parent" android:id="@+id/contactid" android:visibility="invisible"></TextView>
    </LinearLayout>
</LinearLayout>

我使用SimpleCursorAdapter以下列方式填充Listview …

Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.disPLAY_NAME);

        String from[] = new String[]{ContactsContract.CommonDataKinds.Phone.disPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
        int to[] = new int[]{R.id.name,R.id.phone,R.id.contactid};
        SimpleCursorAdapter s = new SimpleCursorAdapter(this,R.layout.contactlayout,c,from,to);
        lv.setAdapter(s);

单击按钮我正在读取所有复选框的状态.问题是,如果我检查一个CheckBox,其他几行就会自动检查.我知道这是重用Views.我该如何避免呢?在这种情况下我甚至没有覆盖getView()方法,所以我想知道是否仍有任何方法可以实现我想要的东西?

回答

最后我实现了@sastraxi建议的内容……

@Override
    public View getView(int position,View convertView,ViewGroup parent) {
        View view = super.getView(position,convertView,parent);

        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
        final TextView name = (TextView) view.findViewById(R.id.name);
        final TextView contactId = (TextView) view.findViewById(R.id.contactid);
        final int pos = position;
        checkBox.setonClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Todo Auto-generated method stub
                if(checkBox.isChecked())
                {
                    checkList.add(String.valueOf(pos));
                    nameList.add(name.getText().toString());
                    contactList.add(contactId.getText().toString());
                    Log.i("Chk added",String.valueOf(pos));
                }
                else
                {
                    checkList.remove(String.valueOf(pos));
                    nameList.remove(name.getText().toString());
                    contactList.remove(contactId.getText().toString());
                    Log.i("Un Chk removed",String.valueOf(pos));
                }
            }
        });

        if(checkList.contains(String.valueOf(pos)))
        {
            checkBox.setChecked(true);
        }
        else
        {
            checkBox.setChecked(false);
        }

        return view;
    }

解决方法

啊,我明白了问题是什么.

创建一个扩展SimpleCursorAdapter的新类,比如CheckBoxSimpleCursorAdapter,并覆盖getView:

public View getView(int position,ViewGroup parent) {
    View view = super.getView(position,parent);
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.CheckBox1);
    checkBox.setChecked(getIsThisListPositionChecked(position));
    return view;
}

由于您没有使用顶级View实现Checkable的布局,您必须自己完成所有操作.这包括清除状态(在这种情况下),因为认实现重新使用了已检查的视图 – 正如您正确直观的那样.

编辑:使用这个新代码,并实现一个受保护的布尔值getIsThisListPositionChecked(int position)方法,该方法返回当前是否检查该项目(或类似的东西).我希望我足够清楚 – 你需要弄清楚是否应该根据你的模型检查项目,然后在创建视图时设置它.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...