强制列表视图不重用的意见(复选框) [英] Force Listview not to reuse views (Checkbox)

查看:144
本文介绍了强制列表视图不重用的意见(复选框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个自定义的列表视图(不覆盖 getView()法)中有一个列表视图每个项目以下布局

contactlayout.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=横向
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent机器人:weightSum =1>
    <复选框机器人:ID =@ + ID / checkBox1机器人:文本=复选框的android:layout_width =134dp机器人:layout_height =108dp机器人:可聚焦=假>< /复选框>
    <的LinearLayout机器人:ID =@ + ID / linearLayout1机器人:layout_height =match_parent机器人:方向=垂直的android:layout_width =87dp机器人:layout_weight =0.84机器人:weightSum =1&GT ;
        <的TextView的android:文本=TextView的机器人:layout_height =WRAP_CONTENT的android:layout_width =match_parent机器人:ID =@ + ID /名称机器人:layout_weight =0.03>< / TextView的>
        <的TextView的android:文本=TextView的机器人:layout_width =match_parent机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID /电话>< / TextView的>
        <的TextView的android:文本=TextView的机器人:layout_height =WRAP_CONTENT机器人:layout_weight =0.03的android:layout_width =match_parent机器人:ID =@ + ID /的ContactID机器人:知名度=隐形&GT ;< / TextView的>
    < / LinearLayout中>
< / LinearLayout中>

我填充列表视图使用 SimpleCursorAdapter 以下列方式...

 光标C = getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)。        从[]字符串=新String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
        诠释为[] =新INT [] {R.id.name,R.id.phone,R.id.contactid};
        SimpleCursorAdapter S =新SimpleCursorAdapter(这一点,R.layout.contactlayout,C,从,到);
        lv.setAdapter(多个);

在点击按钮我读所有的复选框的状态。问题是,如果我选中一个复选框其他几人的路线都会自动选中。我知道这是重用观。我该如何避免呢?我连覆盖在这种情况下 getView()方法,所以我不知道是否还有什么办法达到我想要的?

最后我实现什么@sastraxi建议...

  @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看查看= super.getView(位置,convertView,父母);        最后复选框复选框=(复选框)view.findViewById(R.id.checkBox1);
        最终的TextView名=(的TextView)view.findViewById(R.id.name);
        最终的TextView的ContactID =(TextView中)view.findViewById(R.id.contactid);
        最终诠释POS =位置;
        checkBox.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                如果(checkBox.isChecked())
                {
                    checkList.add(将String.valueOf(POS));
                    nameList.add(name.getText()的toString());
                    contactList.add(contactId.getText()的toString());
                    Log.i(清洁香港加入,将String.valueOf(POS));
                }
                其他
                {
                    checkList.remove(将String.valueOf(POS));
                    nameList.remove(name.getText()的toString());
                    contactList.remove(contactId.getText()的toString());
                    Log.i(联合国清洁香港删除,将String.valueOf(POS));
                }
            }
        });        如果(checkList.contains(将String.valueOf(POS)))
        {
            checkBox.setChecked(真);
        }
        其他
        {
            checkBox.setChecked(假);
        }        返回视图。
    }


解决方案

啊,我看是什么问题。

请扩展的新类 SimpleCursorAdapter ,说 CheckboxSimpleCursorAdapter ,并覆盖 getView 这样:

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看查看= super.getView(位置,convertView,父母);
    复选框复选框=(复选框)view.findViewById(R.id.C​​heckBox1);
    checkBox.setChecked(getIsThisListPositionChecked(位置));
    返回视图。
}

如果你还没有使用布局的顶级查看工具可勾选,你所要做的一切你自己。这包括清除状态(这种情况下),由于使用的默认情况下再实施查看被选中 - 正如你正确直觉

编辑:的使用这个新的code和实施保护布尔getIsThisListPositionChecked(INT位置)返回与否的方法目前项目被选中(或类似的东西)。我希望我是再清楚不过 - 你需要弄清楚,如果该项目的的根据进行检查的的模型,然后设置,当你创建查看

I made a custom Listview (Without overriding getView() method) with each item in a Listview having a following Layout

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>

I am populating the Listview using a SimpleCursorAdapter in a following way...

Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,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);

On Click of a button I am reading the states of all the Checkboxes. The problem is, if I check one CheckBox several others down the line get automatically Checked. I know this is reusing of Views. How do I avoid it ?. I am not even overriding getView() method in this case, so I wonder if there is still any way to achieve what I want?

Answer

Finally I implemented what @sastraxi suggested...

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

解决方案

Ah, I see what the problem is.

Make a new class that extends SimpleCursorAdapter, say CheckboxSimpleCursorAdapter, and override getView as such:

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

As you're not using a layout whose top-level View implements Checkable, you have to do everything yourself. That includes clearing state (in this case), as the default implementation re-used a View that was checked--as you correctly intuited.

Edit: use this new code, and implement a protected boolean getIsThisListPositionChecked(int position) method that returns whether or not the item is currently checked (or something like that). I hope I'm being clear enough--you need to figure out if the item should be checked according to your model, and then set that when you create the View.

这篇关于强制列表视图不重用的意见(复选框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆