CheckBox in ListView being reset when it leaves the screen

后端 未结 2 1662
-上瘾入骨i
-上瘾入骨i 2021-01-27 06:32

I have followed the tutorial here to create a custom ListView that shows items with category headers. I have modified the list_item_entry.xml to put a

2条回答
  •  心在旅途
    2021-01-27 07:17

    class Item{
     boolean isSection;
     String title;
     boolean isOptionChecbox;
    
     //your getter/setter
    
        @Override
        public String toString() {
            return title;
        }
    }
    
    you Adapter:
    public class listAdapter extends ArrayAdapter {
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Item i = items.get(position);
        if(i.isSection()){
                  convertView = vi.inflate(R.layout.list_item_section, parent, false);
                  convertView.setOnClickListener(null);
                  convertView.setOnLongClickListener(null);
                  convertView.setLongClickable(false);
                  final TextView sectionView = (TextView) convertView.findViewById(R.id.list_item_section_text);
                  sectionView.setText(si.getTitle());
           } else{
                  convertView = vi.inflate(R.layout. list_item_entry, parent, false);
                  final TextView title = (TextView) convertView.findViewById(R.id.list_item_entry_title);
                  if (title != null) title.setText(ei.getTitle());
    
                  CheckBox optionCheckbox = (CheckBox) convertView.findViewById(R.id.option_checkbox);
                  optionCheckbox.setChecked(ei.isOptionCheckbox());
                  optionCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                          item.setOptionCheckbox(b);
                      }
                });
           }      
        return convertView;
     }
    
    }
    

提交回复
热议问题