选择全部并清除所有选择复选框

时间:2012-10-03 11:39:33

标签: android android-listview

我正在实现android中的默认消息应用程序等通知功能。现在我通过添加复选框来删除多个邮件,其中我使用一个常用复选框来选择列表中的所有邮件。但我无法检查CustomAdpter的getview中的listview复选框。

class customListAdpter extends BaseAdapter {
    private Context ctx;
    CheckBox checkBox;
    TextView sender, message;

    customListAdpter(Context context) {
        this.ctx = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return new NotifiCation().senderlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int pos, View v, ViewGroup arg2) {
        // TODO Auto-generated method stub
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.checkboxlist, null);
            checkBox = (CheckBox) v.findViewById(R.id.btn_chck);
            sender = (TextView) v.findViewById(R.id.text_senderno);
            message = (TextView) v.findViewById(R.id.text_msg);
        }
        sender.setText("" + new NotifiCation().senderlist.toArray()[pos]);
        message.setText("" + new NotifiCation().msglist.toArray()[pos]);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (buttonView.isChecked()) {

                }

            }

        });
        return v;
    }
}

这是我的主要活动。这里我有主要的复选框。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.delete_notifications);
    db = new DB(NotificationsDelete.this);
    notifications = (ListView) findViewById(R.id.list_with_ckbox);
    selectAll = (CheckBox) findViewById(R.id.btn_checkall);
    done = (Button) findViewById(R.id.done_notification_delete);
    notifications.setAdapter(new customListAdpter(con));
    selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton chkbox, boolean arg1) {
            if (chkbox.isChecked() == true) {
                for (int i = 0; i < new NotifiCation().senderlist.size(); i++) {
                    //notifications.setItemChecked(i, true);
                    customListAdpter adpter=new  customListAdpter(con);
                    adpter.checkBox.setChecked(true);
                }
            } else {
                // new customListAdpter(con).checkBox.setChecked(true);
            }

        }
    });
    done.setOnClickListener(this);

}

1 个答案:

答案 0 :(得分:0)

在处理selectAll复选框字段的onCheckedChanged()后尝试调用notifyDataSetChanged()。