繁体   English   中英

滚动列表后,选中带有单选按钮的Android Multiple listView项目

[英]Android Multiple listView item with radiobuttons get selected after scrolling list

我有一个listview,每个项目包含一个textview和一个radiogroup。 单选组包含两个单选按钮。 我的问题是,当我滚动列表视图时,选择了多个联系人(即,在列表视图的不可见区域中处于相同位置的当前联系人被选中),那么我应该如何克服这个问题?

xml file : 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup  android:id="@+id/selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/allow"
            android:paddingRight="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/block"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

</RelativeLayout>

Adapter code : 


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

            final ViewHolder holder;
             if (convertView == null) {
             convertView = getLayoutInflater().inflate(R.layout.lv_contact, null);

             holder = new ViewHolder();
             convertView.setTag(holder);
             holder.rbg = (RadioGroup) convertView.findViewById(R.id.selection);
             holder.rbg.setTag(position);

             holder.rbAllow = (RadioButton) convertView.findViewById(R.id.allow);
             holder.rbBlock = (RadioButton) convertView.findViewById(R.id.block);
             holder.tv_contactNumber = (TextView) convertView.findViewById(R.id.tv_number);
             holder.rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

             @Override
             public void onCheckedChanged(RadioGroup group, int checkedId) {
             Contact contact;
             switch (checkedId) {
             case R.id.allow:

             contact = contactList.get(position);
             contact.setAllow(true);

             break;

             case R.id.block:

             contact = contactList.get(position);
             contact.setAllow(false);

             break;
             default:
             break;
             }

             });

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

             Contact con = contactList.get(position);
             holder.tv_contactNumber.setText(con.getNumber());  

            if (!con.isAllow() && !con.isBlock()) {

            } else {

                if (con.isAllow()) {
                    holder.rbAllow.setChecked(true);
                    holder.rbBlock.setChecked(false);
                } else {
                    holder.rbAllow.setChecked(false);
                    holder.rbBlock.setChecked(true);
                }
            }
                 return convertView;
        }



Contact file : 
public class Contact {

    private String number;

    private boolean Allow;
    private boolean Block;

    public boolean isAllow() {
        return Allow;
    }

    public void setAllow(boolean isAllow) {
        Allow = Allow;
    }

    public boolean isBlock() {
        return Block;
    }

    public void setBlock(boolean isBlock) {
            Block = isBlock;
    }


    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public Contact(String number, boolean allow, boolean block) {
        name = name;
        Allow = allow;
        Block = block;
    }   

}

这是设计使然,因为列表视图中的视图被回收以降低内存消耗。 解决此问题的一种方法是使用提供给列表视图的数据长度创建一个布尔数组,并在复选框上创建一个侦听器,此监听器在单击时将布尔数组中的位置设置为true或false。 然后,在声明侦听器的适配器中,只需使用存储在数组索引中的值初始化每个复选框。

暂无
暂无

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

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