Android applications are often used to develop Android mobile phone contacts. In Android, listview is usually used to present mobile phone contacts. If you want to achieve full selection using checkbox, the default listview does not seem to solve this problem.
In the following steps, you can use a custom layout to select all contacts on your mobile phone. The result is as follows:
1. Create the main interface layout file main. xml containing listview
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
>
<Linearlayout Android: gravity = "bottom"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Listview Android: Id = "@ + ID/lvcontact"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0"/>
<Scrollview Android: gravity = "bottom"
Android: Id = "@ + ID/scroll_bottom" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Linearlayout Android: Id = "@ + ID/rlall"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Orientation = "vertical">
<Linearlayout
Android: paddingbottom = "0.0dip" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Checkbox Android: Id = "@ + ID/cbselectall"
Android: layout_width = "0.0dip"
Android: layout_height = "wrap_content"
Android: layout_weight = "0.5"
Android: text = "select all"/>
</Linearlayout>
</Linearlayout>
</Scrollview>
</Linearlayout>
</Linearlayout>
2. Create a layout file contactlistitem. xml containing the information of a single contact
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Textview Android: Id = "@ + ID/contact_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "18sp"
/>
<Checkbox Android: Id = "@ + ID/multiple_checkbox"
Android: focusable = "false"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>
3. MasterProgramAll program descriptions are commented out in the program
Package com. Demo;
Import java. util. hashmap;
Import Android. App. activity;
Import Android. content. contentresolver;
Import Android. content. context;
Import Android. database. cursor;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. provider. contactscontract;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. checkbox;
Import Android. widget. compoundbutton;
Import Android. widget. linearlayout;
Import Android. widget. listview;
Import Android. widget. simplecursoradapter;
Import Android. widget. textview;
Import Android. widget. adapterview. onitemclicklistener;
Import Android. widget. compoundbutton. oncheckedchangelistener;
Public class main extends activity {
Private checkbox cbselectall;
Boolean blckall = false;
Private myadapter madapter;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main); // load the main layout interface file
Settitle ("Contact List ");
Listview = (listview) findviewbyid (R. Id. lvcontact); // you can obtain the listview;
Cbselectall = (checkbox) findviewbyid (R. Id. cbselectall); // get the all-selected checkbox
Contentresolver = This. getcontentresolver ();
Uri uri = URI. parse ("content: // com. Android. Contacts/contacts"); // find all contacts on the mobile phone
Cursor cursor = contentresolver. Query (Uri, null );
Startmanagingcursor (cursor); // get the cursor of all contacts
// The key part is to use a custom adapter that inherits simplecursoradapter to display the contact information,
// Add a checkbox selection box after each contact name
// R. layout. contactlistitem is the layout XML of each contact in listiview, and cursor is the cursor data of all contacts.
// String [] indicates all data columns to be rendered in listview,
// Int [] is the ID of the control used to display the data in string [] In contactlistitem.
// Because the BindView method is rewritten to bind data to the layout file, the control focus of the display is in the BindView method.
Madapter = new myadapter (getapplicationcontext (), R. layout. contactlistitem, cursor,
New String [] {contactscontract. Contacts. _ id, contactscontract. Contacts. display_name, contactscontract. Contacts. display_name },
New int [] {R. Id. contact_name, R. Id. contact_name, R. Id. multiple_checkbox });
Listview. setadapter (madapter );
// When selecting all at the bottom, switch the selected status of all checkpoints.
Cbselectall. setoncheckedchangelistener (New oncheckedchangelistener (){
@ Override
Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){
If (ischecked)
{
For (INT I = 0; I <madapter. getcount (); I ++) // set all the maps in the adapter to true;
{
Madapter. Map. Put (I, true );
}
}
Else
{
For (INT I = 0; I <madapter. getcount (); I ++) // set all the maps in the adapter to false;
{
Madapter. Map. Put (I, false );
}
}
Listview = (listview) findviewbyid (R. Id. lvcontact); // you can obtain the listview;
Listview. setadapter (madapter );
}
});
// Add a click event listener for listview
Listview. setonitemclicklistener (New onitemclicklistener (){
@ Override
Public void onitemclick (adapterview <?> AV, view V, int position,
Long ID ){
Checkbox = (checkbox) v. findviewbyid (R. Id. multiple_checkbox );
Checkbox. Toggle ();
If (checkbox. ischecked ())
{
Madapter. Map. Put (Position, true );
}
Else
{
Madapter. Map. Put (Position, false );
}
}
});
}
Public class myadapter extends simplecursoradapter
{
Private layoutinflater minflater;
// Define a hashma to store the number of each contact and a bool value. The Boolean value is used to determine whether checkbox is selected.
Hashmap <integer, Boolean> map = new hashmap <integer, Boolean> ();
// The constructor cyclically traverses the hashmap so that all contacts are out of the unselected state by default.
Public myadapter (context, int layout, cursor C, string [] from,
Int [] ){
Super (context, layout, C, from, );
For (INT I = 0; I <C. getcount (); I ++)
{
Map. Put (I, false );
}
}
// Use BindView to bind data to a custom Layout File
Public void BindView (view, context, cursor ){
// Obtain the layout file of each item
Linearlayout LL = NULL;
If (view = NULL ){
// Use contactlistitem. XML to layout the file
LL = (linearlayout) minflater. Inflate (R. layout. contactlistitem, null );
} Else {
LL = (linearlayout) view;
}
// Use cursor to obtain the columnindex of each field
Int idcol = cursor. getcolumnindex (contactscontract. Contacts. _ id );
// Obtain the contact name
Int namecol = cursor. getcolumnindex (contactscontract. Contacts. display_name );
Final int ncount = cursor. getposition ();
Textview txtname = (textview) ll. findviewbyid (R. Id. contact_name); // contact name display control
String contactid = cursor. getstring (idcol); // obtain the contact ID;
Txtname. settext (cursor. getstring (namecol) + "");
Final checkbox cksel = (checkbox) ll. findviewbyid (R. Id. multiple_checkbox );
// Respond to the checkbox in the contact list. When you click it, the checkbox status of the selected row is switched.
Cksel. setoncheckedchangelistener (New oncheckedchangelistener (){
@ Override
Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){
If (ischecked)
{
Map. Put (ncount, true );
}
Else
{
Map. Put (ncount, false );
}
}
});
// Set whether the checkbox is selected based on the Boolean value of the current row in hashmap.
Cksel. setchecked (Map. Get (ncount ));
}
}
}
4. Modify androidmanifest. XML to add the permission to read the contact list.
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
Start the simulator and run the program to see the effect.