简体   繁体   中英

Android Multiple listView item with radiobuttons get selected after scrolling list

I have listview with each item contains one textview & one radiogroup. Radiogroup contain two radio buttons. My question is when i scroll the listview multiple contact get selected(ie Contact present at same position in invisible area of listview get selected) so how should i overcome this plz help me?

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

}

This occurs by design because the views in a listview are recycled to lower memory consumptions. One way around this is to create a boolean array with the length of the data provided to the listview and create a listener on the checkboxes which sets that position in the boolean array to either true or false when clicked. Then in your adapter before the listener is declared, just initialise each checkbox with the value stored in the index of your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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