将listview中的复选框限制为3

时间:2016-07-29 05:17:19

标签: android listview

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   planet.setChecked(isChecked);
                    if(isChecked)
                    {
                        counter=counter+1;
                        Toast.makeText(getContext(),String.valueOf(counter),Toast.LENGTH_LONG).show();


                    }
                    else
                    {
                        if(counter>3)
                        {
                            counter=3;
                        }
                        counter=counter-1;
                        Toast.makeText(getContext(),String.valueOf(counter),Toast.LENGTH_LONG).show();


                    }
                    if(counter>3)
                    {
                        checkBox.setChecked(false);

                    }

                }
            });

我在getView方法中使用了上面的代码它在列表中正常工作但是当列表滚动时onCheckedChanged方法被调用

2 个答案:

答案 0 :(得分:1)

试试这个适合你的适配器

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<MyModelClass> listData;

    public CustomAdapter(Context mainActivity, List<MyModelClass> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_name);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_check);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getLISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(MyModelClass item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 3;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

答案 1 :(得分:0)

这是您在活动中停留的另一种方式。 (我只是自己写的:D)

在班级中:

private final static int CHECKED_BOX_MAXIMUM = 3;

private boolean isCheckingAllowed(CheckBox checkBox) {
    int checkedBoxes = 0;

    //if a checkbox is checked, count +1
    if (mCheckbox1.isChecked())
        checkedBoxes ++;
    if (mCheckbox2.isChecked())
        checkedBoxes ++;
    if (mCheckbox3.isChecked())
        checkedBoxes ++;
    if (mCheckbox4.isChecked())
        checkedBoxes ++;

    //after the checkbox in the parameter is checked, are there more than 3 checkboxes?
    if(checkedBoxes <= CHECKED_BOX_MAXIMUM) {
        return true;
    }

    //else
    Toast.makeText(getApplicationContext(), "You can only check " + CHECKED_BOX_MAXIMUM + " boxes.", Toast.LENGTH_LONG).show();
    return false;
}

在OnCreate中:

    mCheckbox1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isCheckingAllowed(mCheckbox1))
                mCheckbox1.setChecked(false);
        }
    });
    mCheckbox2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isCheckingAllowed(mCheckbox2))
                mCheckbox2.setChecked(false);
        }
    });
    mCheckbox3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isCheckingAllowed(mCheckbox3))
                mCheckbox3.setChecked(false);
        }
    });
    mCheckbox4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isCheckingAllowed(mCheckbox4))
                mCheckbox4.setChecked(false);
        }
    });