删除列表视图中的已检查项目

时间:2013-04-23 11:03:54

标签: android listview

我想创建一个应用程序,其中包含一个带有复选框的listView和一个名为adddelete selected的两个按钮。我想删除列表视图中检查的所有项目。尽管付出了很多努力,但我无法做到这一点。任何帮助将不胜感激。

这是我的代码

package com.example.chkbokinlistview;

public class Adapter extends ArrayAdapter<Movies> {

    ArrayList<Movies> data;
    Context context;
    int id;
    private Holder h;

    public Adapter(Context context, int textViewResourceId, ArrayList<Movies> objects) {
        super(context, textViewResourceId, objects);
        this.data = objects;
        this.context = context;
        this.id = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int p = position;
        View v = convertView;
        LayoutInflater l = ((Activity)context).getLayoutInflater();
        h = new Holder();
        if (v == null) {
            v = l.inflate(id, parent, false);
            h.tv = (TextView) v.findViewById(R.id.textView1);
            h.cb = (CheckBox) v.findViewById(R.id.checkBox1);
            v.setTag(h);
        }else{
            h = (Holder) v.getTag();
            h.cb.setChecked(true);
        }
        h.tv.setText(data.get(position).movieName);
        h.cb.setChecked(data.get(position).deleted);
        return v;
    }
    public void delete(){
        //how to delete all the items that are checked
    }

     class Holder{
        TextView tv;
        CheckBox cb;
    }

}

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        bDelete = (Button) findViewById(R.id.bDelete);
        bAdd = (Button) findViewById(R.id.bAdd);

        list = new ArrayList<Movies>();
        a = new Adapter(this, R.layout.listitem, list);
        lv.setAdapter(a);

        bDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                a.delete();
            }
        });
        bAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                final EditText et = new EditText(MainActivity.this);
                dialog = new AlertDialog.Builder(MainActivity.this)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        list.add(new Movies(et.getText().toString(), false));
                        a.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                })
                .setTitle("ADD Movie")
                .setView(et)
                .create();
                dialog.show();
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

选中checkbox position ArrayList后添加toBeDeleted,然后点击删除按钮,只需删除ArrayList中的项目根据您在data中的位置命名为toBeDeleted,并调用适配器方法notifyDataSetChanged()


checkedChangedListener getView方法添加CheckBox

h.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean arg1) {
                        // TODO Auto-generated method stub
                        if (arg1) {
                            list.add(position);
                        } else {
                            for (int i = 0; i < list.size(); i++) {
                                if (list.get(i) == position) {
                                    list.remove(i);
                                    break;
                                }
                            }
                        }

                    }
                });

列表是ArrayList<Integer>, 并删除

private void delete() {
    for(int i = 0 i<list.size;i++)
        data.remove(list.get(i));
}

但在删除之前,您必须按降序对list进行排序,以便正确删除,否则您可能会获得IndexOutofBoundException