Android listview checkbox radio selection
为情所困
为情所困 2017-05-16 13:29:21
0
2
718

Now the background returns a value of whether the default address is int Isdefault = bean.getDefault_address();


How to implement checkbox switching?

为情所困
为情所困

reply all(2)
为情所困

You can write like this with minimal changes to your code. First find the address selected by default

int defaultPosition = -1;
for (int i = 0; i < beanlist.size(); i++) {
     if(beanlist.get(i).getDefault_address()){
         defaultPosition = i;
         break;
     }
}

Two situations:

  1. After selecting another address and then canceling it, the default address will be re-selected

if(tempPosition > -1){
    if(position == tempPosition){
        holder.checkBox.setChecked(true);
    }else{
        holder.checkBox.setChecked(false);
    }
}else{
    if(position == defaultPosition){
        holder.checkBox.setChecked(true);
    }else{
        holder.checkBox.setChecked(false);
    }
}
  1. Once you select another address, the default option will no longer work

tempPosition = defaultPosition;
if(position == tempPosition){
    holder.checkBox.setChecked(true);
}else{
    holder.checkBox.setChecked(false);
}
淡淡烟草味

It is easier to create an array to maintain the status of checkBox.
When CheckBox receives a click event, change the status of the corresponding checkBox in statusList, and at the same time set the status of checkBox according to the value of statusList in the onBindView/getView method.

---------------------Separating line------------------------- -------------------------------------------------- ------
I didn’t see that it requires single selection. . For single selection, you need to add a flag to record the last selected position, then get the object from the adapter, and modify the checkStatus if it is visible.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template