如何在Android中选中CheckBox时删除元素?

时间:2014-09-28 09:03:49

标签: android listview

我的代码:

public class InteractiveArrayAdapter extends ArrayAdapter<DataModel> {

    private final List<DataModel> list, list1;
    private final Activity context;
    private ArrayList<DataModel> callList, smsList;

    public InteractiveArrayAdapter(Activity context, List<DataModel> list,
            List<DataModel> list1) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
        this.list1 = list1;
        callList = new ArrayList<DataModel>();
        smsList = new ArrayList<DataModel>();
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox, checkbox1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);

            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            if (isChecked) {
                                if (!callList.contains((String) list.get(
                                        position).getName())) {
                                    callList.add(new DataModel((String) list
                                            .get(position).getNumber(),
                                            (String) list.get(position)
                                                    .getName()));
                                    DataModel element = (DataModel) viewHolder.checkbox
                                            .getTag();

                                    viewHolder.checkbox.setChecked(true);
                                }

                            } else {

                                callList.remove(new DataModel((String) list
                                        .get(position).getNumber(),
                                        (String) list.get(position).getName()));
                                DataModel element = (DataModel) viewHolder.checkbox
                                        .getTag();
                                Toast.makeText(context,
                                        "Remove SMS size:" + callList.size(),
                                        Toast.LENGTH_LONG).show();
                                viewHolder.checkbox.setChecked(false);
                            }

                        }
                    });
            viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.check1);
            viewHolder.checkbox1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            DataModel element = (DataModel) viewHolder.checkbox1
                                    .getTag();
                            Toast.makeText(context,
                                    "Call size:" + list1.size(),
                                    Toast.LENGTH_LONG).show();

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
            viewHolder.checkbox1.setTag(list1.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox1.setTag(list1.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        return view;
    }
}

在检查CkechBox时成功添加ArrayList,但是当我取消选中checkBox然后不从arrayList中删除。请帮我。我在stackoverflow中尝试了更多。没有找到任何解决方案。请帮帮我。

0 个答案:

没有答案