Android在listview添加checkbox实现
主界面CheckBoxinListViewActivity.java代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class CheckBoxinListViewActivity extends Activity { 2 /** Called when the activity is first created. */ 3 4 private MyAdapter adapter; 5 private ListView listview; 6 private Button checkAll; 7 private Button noCheckAll; 8 9 10 @Override 11 public void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.main); 14 listview = (ListView)findViewById(R.id.listview); 15 checkAll = (Button)findViewById(R.id.button1); 16 noCheckAll = (Button)findViewById(R.id.button2); 17 adapter = new MyAdapter(); 18 listview.setAdapter(adapter); 19 checkAll.setOnClickListener(new OnClickListener() { 20 21 @Override 22 public void onClick(View v) { 23 adapter.checkAll(); 24 } 25 }); 26 noCheckAll.setOnClickListener(new OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 adapter.noCheckAll(); 31 } 32 }); 33 34 } 35 36 37 38 private class MyAdapter extends BaseAdapter{ 39 40 private ArrayList<Message> list= new ArrayList<Message>(); 41 42 public MyAdapter(){ 43 for(int i = 1 ;i<=100 ; i++){ 44 list.add(new Message("item_"+i)); 45 } 46 } 47 48 public void checkAll(){ 49 for(Message msg:list){ 50 msg.isCheck = true; 51 } 52 notifyDataSetChanged(); 53 } 54 public void noCheckAll(){ 55 for(Message msg:list){ 56 msg.isCheck = false; 57 } 58 notifyDataSetChanged(); 59 } 60 61 @Override 62 public int getCount() { 63 return list.size(); 64 } 65 66 @Override 67 public Object getItem(int position) { 68 return null; 69 } 70 71 @Override 72 public long getItemId(int position) { 73 return 0; 74 } 75 76 @Override 77 public View getView(int position, View convertView, ViewGroup parent) { 78 ViewHolder viewHolder; 79 if(convertView == null){ 80 LayoutInflater inflater = LayoutInflater.from(CheckBoxinListViewActivity.this); 81 convertView = inflater.inflate(R.layout.listview_item, null); 82 viewHolder = new ViewHolder(); 83 viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1); 84 convertView.setTag(viewHolder); 85 }else{ 86 viewHolder = (ViewHolder)convertView.getTag(); 87 } 88 final Message msg = list.get(position); 89 viewHolder.checkBox.setText(msg.str); 90 viewHolder.checkBox.setChecked(msg.isCheck); 91 //注意这里设置的不是onCheckedChangListener,还是值得思考一下的 92 viewHolder.checkBox.setOnClickListener(new OnClickListener() { 93 94 @Override 95 public void onClick(View v) { 96 if(msg.isCheck){ 97 msg.isCheck = false; 98 }else{ 99 msg.isCheck = true; 100 } 101 102 } 103 }); 104 return convertView; 105 } 106 107 } 108 private class ViewHolder{ 109 CheckBox checkBox; 110 } 111 }
适配器所适配的消息Message.java如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Message { 2 public boolean isCheck; 3 public String str; 4 public Message(String str){ 5 this.str = str; 6 } 7 }
main.xml代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <LinearLayout 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content"> 8 <Button android:text="全选" android:id="@+id/button1" 9 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 10 <Button android:text="撤消" android:id="@+id/button2" 11 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 12 </LinearLayout> 13 <ListView android:id="@+id/listview" android:layout_height="fill_parent" 14 android:layout_width="fill_parent" /> 15 16 </LinearLayout>
listview_item.xml代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:descendantFocusability="blocksDescendants" 6 > 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:gravity="center"> 11 <CheckBox 12 android:text="CheckBox" 13 android:id="@+id/checkBox1" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content"/> 16 </LinearLayout> 17 </LinearLayout>
非著名程序员可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程,微信号:smart_android。