checkbox 点击选中的监听事件,只能从最后选中的地方开始监听,不知道自己错在哪里,大神们帮帮忙吧!

public class SmsDataAdapter extends ArrayAdapter {
private Context context;
private ArrayList lists;
CheckBox cb;
public SmsDataAdapter(Context context, ArrayList lists) {
super(context,android.R.layout.simple_list_item_1,lists);
this.context = context;
this.lists = lists;
}
public ArrayList getCheckedItems(){
ArrayList checkedList = new ArrayList<>();
for (SmsData smsData : lists){
if (smsData.isChecked()) {
checkedList.add(smsData);
}
}

    return checkedList;

}

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v==null){
        v = LayoutInflater.from(context).inflate(R.layout.list_item_view,null,false);
    }
    TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
    TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);
    TextView tv_content = (TextView) v.findViewById(R.id.tv_content);
    cb= (CheckBox) v.findViewById(R.id.checkBox);

    SmsData smsData = lists.get(position);
    cb.setChecked(smsData.isChecked());
    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (cb.isChecked()){
                lists.get(position).setChecked(true);
            }
        }
    });

    tv_name.setText(smsData.getName());
    tv_phone.setText(smsData.getPhone());
    tv_content.setText(smsData.getSms());

    if (smsData.isSent()){
        tv_content.setTextColor(Color.BLUE);
    }else {
        tv_content.setTextColor(Color.BLACK);
    }
    return v;
}

public void setAllChecked(){
    for (SmsData smsData:lists){
        smsData.setChecked(true);
    }
    notifyDataSetChanged();
}
public void clearAllChecked(){
    for (SmsData smsData:lists){
        smsData.setChecked(false);
    }
    notifyDataSetChanged();
}
public int getCheckedCount(){
    int count = 0;
    for (int i = 0;i<lists.size();i++){
        if (lists.get(i).isChecked()) {
            count++;
        }
    }
    return count;
}

}


1个回答

不是只能监听最后选中的地方,是你在监听中只判断选中的选项
if (cb.isChecked()){
lists.get(position).setChecked(true);
}
你改为
lists.get(position).setChecked(b);
再试试,看看有变化没

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!