weixin_44154472 2018-12-27 23:43 采纳率: 0%
浏览 2989

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条回答

  • 开发小学生 2018-12-29 03:22
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条