search

Home  >  Q&A  >  body text

Android listview checkbox radio selection

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


How to implement checkbox switching?

为情所困为情所困2768 days ago745

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-16 13:31:21

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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:31:21

    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.

    reply
    0
  • Cancelreply