如何使用android中的自定义arrayadapter使用listview中的复选框选中后删除多个项目?

时间:2015-04-20 06:28:20

标签: android checkbox android-listview android-arrayadapter

我有一个带有1个复选框和2个标签的Listview。我为Listview使用了Custom ArrayAdapter类。我使用勾选的复选框成功删除了列表视图中的项目,但问题是没有正确删除多个项目。

  public class MainActivity extends Activity {

        ListView listView;
        Button btnDelete;
        ArrayList<String> items = new ArrayList<String>();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        CustomAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            listView = (ListView) findViewById(R.id.listView1);
            btnDelete = (Button) findViewById(R.id.btnDelete);

            for (int i = 0; i < 20; i++) {
                items.add("Chk " + i);
            }

            adapter = new CustomAdapter(MainActivity.this, R.layout.custome_list,
                    items);
            listView.setAdapter(adapter);

            btnDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (ids.size() > 0) {
                        for (int i = 0; i < ids.size(); i++) {
                            items.remove(items.get(ids.get(i)));

                        }

                        adapter.notifyDataSetChanged();
                    }
                }
            });
        }

        public class CustomAdapter extends ArrayAdapter<String> {

            List<String> items;
            int resource;

            public CustomAdapter(Context context, int resource, List<String> items) {
                super(context, resource, items);
                this.items = items;
                this.resource = resource;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                final int pos = position;
                ViewHolder holder = null;
                if (convertView == null) {
                    holder = new ViewHolder();
                    LayoutInflater inflater = getLayoutInflater();

                    convertView = inflater.inflate(resource, null);

                    holder.chk = (CheckBox) convertView.findViewById(R.id.chk);
                    holder.txt = (TextView) convertView.findViewById(R.id.txt);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.txt.setText(items.get(position));

                holder.chk
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(CompoundButton buttonView,
                                    boolean isChecked) {

                                if (isChecked) {
                                    ids.add(position);
                                    System.out.println("IDS A: " + ids.toString());
                                } else {
                                    if (ids.contains(position)) {
                                        int i = ids.indexOf(position);
                                        ids.remove(i);
                                        System.out.println("IDS R: "
                                                + ids.toString());
                                    }
                                }

                            }
                        });
                return convertView;
            }
        }

        public class ViewHolder {
            CheckBox chk;
            TextView txt;
        }
    }

1 个答案:

答案 0 :(得分:1)

重新使用convertView时,必须重新加载检查状态。 setOnCheckedChangeListener(null)很重要,因为在设置检查状态时将调用旧的侦听器。

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

    ViewHolder holder = null;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = getLayoutInflater();

        convertView = inflater.inflate(resource, null);

        holder.chk = (CheckBox) convertView.findViewById(R.id.chk);
        holder.txt = (TextView) convertView.findViewById(R.id.txt);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt.setText(items.get(position));

    // you have to reload the check states
    holder.chk.setOnCheckedChangeListener(null);
    holder.chk.setChecked(ids.contains(position));

    holder.chk
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        ids.add(position);
                        System.out.println("IDS A: " + ids.toString());
                    } else {
                        if (ids.contains(position)) {
                            int i = ids.indexOf(position);
                            ids.remove(i);
                            System.out.println("IDS R: "
                                    + ids.toString());
                        }
                    }

                }
            });
    return convertView;
}