Listview + checkbox

前端 未结 2 1825
醉梦人生
醉梦人生 2021-01-27 04:03

I\'m trying to do a listview with a checkbox but i can\'t do it. I don\'t know how i can implement the listener that tell me if the ckeckbox is selected or isn\'t selected.

相关标签:
2条回答
  • 2021-01-27 04:28

    Check this tutorial it explains holder pattern in listView Tutorial

    Also remember that you need to assign any onClickListener or onCheckListener in getView() method of your adapter

    0 讨论(0)
  • 2021-01-27 04:29

    In getView(...) you have to use CompoundButton.OnCheckedChangeListener try to use ViewHolder pattern

    • Tutorial on listview with check box

    code snippet

    viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);
         viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                  TemaRescatado element = (TemaRescatado) viewHolder.checkbox
                      .getTag();
                  element.setSelected(buttonView.isChecked());
    
                }
              });
    
    0 讨论(0)
提交回复
热议问题