主界面CheckBoxinListViewActivity.java代码如下:
[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<Message> list= new ArrayList<Message>();
public MyAdapter(){
for(int i = 1 ;i<=100 ; i++){
list.add(new Message("item_"+i));
}
}
public void checkAll(){
for(Message msg:list){
msg.isCheck = true;
}
notifyDataSetChanged();
}
public void noCheckAll(){
for(Message msg:list){
msg.isCheck = false;
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(CheckBoxinListViewActivity.this);
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
补充:移动开发 , Android ,