ListView Checkbox problem in CustomAdapter

前端 未结 1 1243
无人及你
无人及你 2021-01-20 03:04

friends,

i am using following code to check/uncheck checkboxs inside listview

public AdapterBank(Context context,List list,ListView lst)          


        
相关标签:
1条回答
  • 2021-01-20 03:49

    You could try to have a static listener (might not correct your bug but will surely lower the number of "temp" objects you are creating):

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_banks, null);
            holder = new ViewHolder();
            holder.bankName = (TextView) convertView.findViewById(R.id.bankName);
            holder.bankIcon = (ImageView) convertView.findViewById(R.id.bankIcon);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            holder.checkBox.setOnCheckedChangeListener(checkListener);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        Bank bank = getItem(position);
        holder.checkBox.setTag(bank);
        holder.checkBox.setChecked(bank.isSelected());
    
        return convertView;
    }
    
    private OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {
         Bank b = (Bank) checkboxView.getTag();
         b.setSelected(isChecked);
       }
    });
    
    0 讨论(0)
提交回复
热议问题