checkbox的全选、反选、删除(MainActivity)
package com.example.ay;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.LVadapter;
import com.example.vo.Flag;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
public class MainActivity extends Activity {
List<Flag> list = new ArrayList<Flag>();
private LVadapter lv;
private ListView listview;
private List<Flag> li;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 13; i++) {
list.add(new Flag(false, "单面镜" + i));
}
init();
}
private void init() {
listview = (ListView) findViewById(R.id.listview);
lv = new LVadapter(list, MainActivity.this);
listview.setAdapter(lv);
CheckBox checkall = (CheckBox) findViewById(R.id.checkAll);
CheckBox fanCheck = (CheckBox) findViewById(R.id.fanCheck);
Button button = (Button) findViewById(R.id.button);
li = new ArrayList<Flag>();
// 对删除按钮进行监听
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*for (Flag ff : list) {
if (ff.isFlag()) {
li.add(ff);
}
}
lv.notifyDataSetChanged();
list.removeAll(li);*/
Iterator it=list.Iterator();
while(it.hasNext()){
Flag next=(Flag)it.next();
if(next.isFlag()){
it.remove();
}
}
lv.notifyDataSetChanged();
}
});
checkall.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
for (Flag f : list) {
f.setFlag(isChecked);
lv.notifyDataSetChanged();
}
}
});
fanCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
for (Flag f : list) {
if (f.isFlag()) {
f.setFlag(false);
} else {
f.setFlag(true);
}
}
lv.notifyDataSetChanged();
}
});
}
}