当前位置: 编程技术>移动开发
Android在listview添加checkbox实现原理与代码
来源: 互联网 发布时间:2014-10-12
本文导语: 主界面CheckBoxinListViewActivity.java代码如下: 代码如下: public class CheckBoxinListViewActivity extends Activity { /** Called when the activity is first created. */ private MyAdapter adapter; private ListView listview; private Button checkAll; private Button noCheckAll; @Override pu...
主界面CheckBoxinListViewActivity.java代码如下:
代码如下:
public class CheckBoxinListViewActivity extends Activity {
/** Called when the activity is first created. */
private MyAdapter adapter;
private ListView listview;
private Button checkAll;
private Button noCheckAll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView)findViewById(R.id.listview);
checkAll = (Button)findViewById(R.id.button1);
noCheckAll = (Button)findViewById(R.id.button2);
adapter = new MyAdapter();
listview.setAdapter(adapter);
checkAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.checkAll();
}
});
noCheckAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.noCheckAll();
}
});
}
private class MyAdapter extends BaseAdapter{
private ArrayList list= new ArrayList();
public MyAdapter(){
for(int i = 1 ;i