Getting an issue while checking the dynamically generated checkbox through list view

后端 未结 4 1092
陌清茗
陌清茗 2020-11-22 06:04

I know that this question is already asked by other members and solution is also given by some members but the thing is that i didnt find any solution which is suitable for

4条回答
  •  無奈伤痛
    2020-11-22 07:07

    Try this,

    Create a POJO class that will maintain the state of the Checkbox selected items like this,

    public class Model {
    
        private String name;
        private boolean selected;
    
        public Model(String name) {
            this.name = name;
            selected = false;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public boolean isSelected() {
            return selected;
        }
    
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    }
    

    And this is the stuff that you have to apply to getView() method in the Adapter.

    public View getView(int position, View convertView, ViewGroup parent) {
    
            checkBoxCounter = 0;      
            checkBoxInitialized = 0;   
            if (convertView == null) {
                final ViewHolder viewHolder = new ViewHolder();
                LayoutInflater inflator = context.getLayoutInflater();
                convertView = inflator.inflate(R.layout.main, null);
                viewHolder.text = (TextView) convertView.findViewById(R.id.label);
                viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
    
                viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Model element = (Model) viewHolder.checkbox.getTag();
                        element.setSelected(buttonView.isChecked());
    
                        if(checkBoxCounter <= checkBoxInitialized){
                        // increment counter, when we scroll the List it execute onCheckedChanged everytime so by using this stuff we can maintain the state
                        checkBoxCounter++;
                    }
                    else{
                        Model element = (Model) viewHolder.checkbox.getTag();
                        element.setSelected(buttonView.isChecked());
    
                        if(element.isSelected())
                        Toast.makeText(getContext(), "You selected "+ element.getName(), Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(getContext(), "Not selected "+ element.getName(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
                convertView.setTag(viewHolder);
                viewHolder.checkbox.setTag(list.get(position));
            } 
            else{
                ((ViewHolder) convertView.getTag()).checkbox.setTag(list.get(position));
            }
    
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.text.setText(list.get(position).getName());
            viewHolder.checkbox.setChecked(list.get(position).isSelected());
            return convertView;
        }
    

    For further study about how this works you can have a look at the complete example. And also you can have a look at How ListView Works

    UPDATE: I had recently added a solution for this type of issue on by blog. ListView with CheckBox Scrolling Issue

提交回复
热议问题