0

我想制作一个应用程序,其中包括一个listView带有复选框和一个名为addand的两个按钮delete 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();
            }
        });
    }
4

1 回答 1

1

在检查checkbox添加内容时,positionArrayListtoBeDeleted单击删除按钮时,只需根据您所在的位置从您的ArrayList名称中删除项目并调用适配器方法。datatoBeDeletednotifyDataSetChanged()


checkedChangedListenergetView方法中为您的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;
                                }
                            }
                        }

                    }
                });

其中 list 是 a ArrayList<Integer>,用于删除

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

但在删除之前,您必须按list降序排序,以便正确删除,否则您可能会得到一个IndexOutofBoundException

于 2013-04-23T11:29:13.647 回答