Androd listview + checkbox solves the problem that the screen cannot be fully selected.

Source: Internet
Author: User

Go directlyCodeThe main idea is to modify the value in list <string []>.

Activity Type

 1   Package Com. Test. listact;
2
3 Import Java. util. arraylist;
4 Import Java. util. List;
5
6 Import Android. App. activity;
7 Import Android. OS. Bundle;
8 Import Android. View. view;
9 Import Android. widget. Button;
10 Import Android. widget. listview;
11 Import Android. widget. Toast;
12
13 Public Class Listselectalltestactivity Extends Activity {
14
15 Private Listview mlistview;
16 Private Button selbtn, selectedbtn;
17 Private Selectadapter myadapter;
18 Private Boolean Isselectall =False ;
19 Private List <string []> DATA = Null ;
20 Public Void Oncreate (bundle savedinstancestate ){
21 Super . Oncreate (savedinstancestate );
22 Setcontentview (R. layout. Main );
23
24 Mlistview = (listview) findviewbyid (R. Id. listview );
25
26 Data = getdata ();
27
28 Myadapter = New Selectadapter ( This , Data );
29
30 Mlistview. setadapter (myadapter );
31
32 Selbtn = (button) findviewbyid (R. Id. selbtn );
33
34 Selbtn. setonclicklistener ( New Button. onclicklistener (){
35 Public Void Onclick (view arg0 ){
36 If (Isselectall ){
37 Myadapter. cancelall ();
38 Isselectall =False ;
39 } Else {
40 Myadapter. selectall ();
41 Isselectall = True ;
42 }
43 }
44 });
45
46 Selectedbtn = (button) findviewbyid (R. Id. selectedbtn );
47 Selectedbtn. setonclicklistener ( New Button. onclicklistener (){
48 Public Void Onclick (view arg0 ){
49 If (Data! = Null ){
50 String [] D;
51 Int Select = 0, cancel = 0;
52 For (String [] dd: Data ){
53 If (! Dd [1]. Equals ("0") {// '0' indicates not selected
54 Select ++;
55 } Else {
56 Cancel ++;
57 }
58 }
59 Toast. maketext (listselectalltestactivity. This , "Selected quantity:" + select + "\ t unselected quantity:" + cancel, Toast. length_long). Show ();
60 }
61 }
62 });
63 }
64
65 Private List <string []> getdata (){
66 List <string []> List = New Arraylist <string []> ();
67 For ( Int I = 0; I <20; I ++ ){
68 String [] DATA = New String [2];
69 Data [0] = "hello" + (I + 1 );
70 Data [1] = "0 ";
71 List. Add (data );
72 }
73 Return List;
74 }
75
76 }

Adapter class, the main logic here

 Package Com. Test. listact;

Import Java. util. List;

Import Android. content. context;
Import Android. util. log;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. checkbox;
Import Android. widget. compoundbutton;
Import Android. widget. textview;

Public Class Selectadapter Extends Baseadapter {


Private Layoutinflater minflater;
Private Context mcontext;
Private List <string []> MList;

Public Selectadapter (context mcontext, list <string []> MList ){
This . MList = MList;
This . Mcontext = mcontext;
This . Minflater = layoutinflater. From (mcontext );
}

Public Int Getcount (){
Return MList. Size ();
}

Public Object getitem ( Int Arg0 ){
Return MList. Get (arg0 );
}

Public Long Getitemid ( Int Arg0 ){
Return Arg0;
}

Public View getview ( Int Position, view, viewgroup parent ){
Viewhold mviewhold;
If (View = Null ){
Log. D ("", "view vvvvv ");
Mviewhold = New Viewhold ();
View = minflater. Inflate (R. layout. item, Null );
Mviewhold. mtextview = (textview) view. findviewbyid (R. Id. Label );
Mviewhold. mcheckbox = (checkbox) view. findviewbyid (R. Id. labelcheckbox );
View. settag (mviewhold );
} Else {
Mviewhold = (viewhold) view. gettag ();
Log. D ("", "view vvvvveeeeeeee ");
}

String data [] = MList. Get (position );
Mviewhold. mtextview. settext (data [0]);

String isselected = data [1];

Mviewhold. mcheckbox. setid (position );
Mviewhold. mcheckbox. setchecked (! Isselected. Equals ("0 "));

// Modify the value when you click
Mviewhold. mcheckbox. setoncheckedchangelistener ( New Checkbox. oncheckedchangelistener (){

Public Void Oncheckedchanged (compoundbutton buttonview, Boolean Ischecked ){

String dat [] = MList. Get (buttonview. GETID ());
Dat [1] = ischecked? "1": "0 ";
MList. Set (buttonview. GETID (), dat );
}

});



Return View;
}
// Select All
Public Void Selectall (){
If (MList! =Null ){
For ( Int I = 0; I <MList. Size (); I ++ ){
MList. Get (I) [1] = "1 ";
}
This . Notifydatasetchanged ();
}
}
// Cancel all
Public Void Cancelall (){
If (MList! =Null ){
For ( Int I = 0; I <MList. Size (); I ++ ){
MList. Get (I) [1] = "0 ";
}
This . Notifydatasetchanged ();
}
}


Class Viewhold {
Public Textview mtextview;
Public Checkbox mcheckbox;
}

}

The XML file is omitted here...

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.