在Android listivew用户中限制复选框选择Preseed 3 poition list position = 1复选框是自动删除

时间:2016-12-20 09:11:43

标签: android listview android-checkbox

我想在android checkbox中限制listivew选择,例如,当用户选择3个位置chackbox位置时,只选中了2个复选框,一个checkboc未选中。

用户可以从列表中选择任意两个复选框,如何实现?这是我的要点:

code

    public class ListAdapter extends ArrayAdapter<ListModel> {

    private ListModel listModel;
    private int selectedItemCounter = 0;

    public ListAdapter(Context context, List<ListModel> listModels) {
        super(context, 0, listModels);
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        listModel = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
        }
        TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
        TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
        final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
        LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);

        Listener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                icon_right.performClick();
            }
        });
        icon_right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedItemCounter++;
                } else {
                    selectedItemCounter--;
                }
                if (selectedItemCounter >= 3) {
                    buttonView.setChecked(false);
                    selectedItemCounter--;
                    notifyDataSetChanged();
                }
            }
        });


        icon_right.setChecked(false);
        productTitle.setText(listModel.product);
        productDescription.setText(listModel.productDetails);
        // icon_right.setImageResource(R.drawable.icon_save);
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

public class ListAdapter extends ArrayAdapter<ListModel>{

private ListModel listModel;
private ArrayList<Integer> selectedPos = new ArrayList<Integer>();

public ListAdapter(Context context, List<ListModel> listModels) {
    super(context, 0, listModels);
}

@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    listModel = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
    }
    TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
    TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
    final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
    LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);

    Listener.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            icon_right.performClick();
        }
    });
    icon_right.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            if (cb.isChecked()) {
                selectedPos.add((Integer)v.getTag());
            } else {
                selectedPos.remove((Integer)v.getTag());
            }
            if (selectedPos.size() >= 3) {
                selectedPos.remove(0);
                notifyDataSetChanged();
            }
        }
    });

    productTitle.setText(listModel.product);
    productDescription.setText(listModel.productDetails);

    if(selectedPos.contains(position)){
        icon_right.setChecked(true);
    }else{
        icon_right.setChecked(false);
    }

    icon_right.setTag(position);
    return convertView;
}}

希望有所帮助!