CheckBox will uncheck and check when scrolling down the listview

前端 未结 4 1956
庸人自扰
庸人自扰 2021-01-28 10:21

In my application I use this to count the checked checkbox in real time meaning when tick the box the count above will increase or decrease. but when scrolling down the listview

4条回答
  •  有刺的猬
    2021-01-28 10:52

    I guess the problem is not using holder
    make a bean and adapter like @Athira says
    then inside adapter in getView try this

    @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
       ViewHolder holder = null;
    
     if (view == null) {
                LayoutInflater inflater=LayoutInflater.from(context);
                view=inflater.inflate(R.layout.phone_list_item,viewGroup,false);
                holder.phone1= (TextView) view.findViewById(R.id.phone1);
                holder.name1= (TextView) view.findViewById(R.id.name1);
                holder.tick= (CheckBox) view.findViewById(R.id.tick);
     view.setTag(holder);
    }else{
                holder = (ViewHolder) view.getTag();
            }
               holder.phone1.setText(contactModels.get(i).getPhone());
               holder.name1.setText(contactModels.get(i).getName());
                if(contactModels.get(i).isSel())
                {
                    holder.tick.setSelected(true);
                }
                else
                {
                    holder.tick.setSelected(true);
                }
         holder.tick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        contactModels.get(i).setSel(isChecked);
                        notifyDataSetChanged();
                    }
                });
                return view;
            }
     public class ViewHolder {
            TextView phone1,name1;
    CheckBox tick;
    
        }
        }
    

提交回复
热议问题