Implementation principle and code of adding checkbox to listview by Android

Source: Internet
Author: User


The main interface CheckBoxinListViewActivity. java code is as follows:: Copy codeThe Code is as follows: 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 );
} Else {
ViewHolder = (ViewHolder) convertView. getTag ();
}
Final Message msg = list. get (position );
ViewHolder. checkBox. setText (msg. str );
ViewHolder. checkBox. setChecked (msg. isCheck );
// Note that onCheckedChangListener is not set here, so it is worth thinking about.
ViewHolder. checkBox. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
If (msg. isCheck ){
Msg. isCheck = false;
} Else {
Msg. isCheck = true;
}
}
});
Return convertView;
}
}
Private class ViewHolder {
CheckBox checkBox;
}
}

The Message. java applicable to the adapter is as follows:Copy codeThe Code is as follows: public class Message {
Public boolean isCheck;
Public String str;
Public Message (String str ){
This. str = str;
}
}

The main. xml Code is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
<Button android: text = "select all" android: id = "@ + id/button1"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "undo" android: id = "@ + id/button2"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
</LinearLayout>
<ListView android: id = "@ + id/listview" android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"/>
</LinearLayout>

The listview_item.xml code is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: descendantFocusability = "blocksDescendants"
>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center">
<CheckBox
Android: text = "CheckBox"
Android: id = "@ + id/checkBox1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</LinearLayout>

You do not understand the world of the arms, you do not know the life of the arms, and you only know the world of programmers. Programmers, strive for their own wonderful world, and work hard! Come on ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.