Today, I learned to write layout files instead of layout files.ProgramIn the layout file. After I create a checkedtextview control using the layoutinflactor, add the checkedtextview TO THE listview, then, when checkedtextview is set to true or false using setchecked, it is found that the selected status of checkedtextview cannot be changed. You cannot use the checkedtextview method to modify the selected status. (Checkbox has the same problem ).
Add the listview Method to the activity.CodeAs follows:
@ Override
Protected Void Oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super . Oncreate (savedinstancestate );
// Set display
Listview = Getlistview ();
// Instantiation
Li = Getlayoutinflater (); // You can select multiple settings.
Listview. setchoicemode (listview. choice_mode_multiple );
Listview. setadapter ( New Dataadapter ());
}
The code for setting the listview structure is set using the listview. setadapter () method. (Checkedtextvew is set in this), the following shows the most important method of dataadapter getview:
@ Override
Public View getview ( Int Position, view convertview, viewgroup parent ){
// Todo auto-generated method stub
View = Li. Inflate (R. layout. Second, Null );
Switch (Position ){
// Enable address book?
Case 0 :
Ctv1 = (Checkedtextview) Li. Inflate (
Android. R. layout. simple_list_item_multiple_choice, Null );
Ctv1.settext (STR [position]);
If (On_off = 1 ){
Log. I (tag, " On_off = " + On_off );
// Ctv1.setchecked (true ); Listview. setitemchecked (position, True );
} Else { // Ctv1.setchecked (false );
Listview. setitemchecked (position, False );
}
Return Ctv1;
// Enable alarm
Case 2 :
Ctv2 = (Checkedtextview) Li. Inflate (
Android. R. layout. simple_list_item_multiple_choice, Null );
Ctv2.settext (STR [position]);
If (Alarm = 1 ) // Ctv2.setchecked (true );
Listview. setitemchecked (position, True );
Else { // Ctv2.setchecked (false );
Listview. setitemchecked (position, False );
}
Return Ctv2;
Default :
Return Null ;
}
}
}
From the above function, we can see that the red comment section is the checkedtextview control's own view to change its selected status. In this way, in our listview, The checkedtextview display will not change, so the checkedtextview in listview cannot change its status, and the listview has a method of listview. the setitemchecked (long position, Boolean value) method is used to change the selected status of a widget in a certain position of listview. If the widget does not support selection, nothing will be changed. Otherwise, control changes its status.
Look at the androidSource codeThe method of the listview. setitemchecked () function is displayed. The function is as follows:
Public Void Setitemchecked ( Int Position, Boolean Value ){
If (Mchoicemode = Choice_mode_none ){
Return ;
}
If (Mchoicemode = Choice_mode_multiple ){
Mcheckstates. Put (Position, value );
If (Mcheckedidstates ! = Null && Madapter. hasstableids ()){
If (Value ){
Mcheckedidstates. Put (madapter. getitemid (position), Boolean. True );
} Else {
Mcheckedidstates. Delete (madapter. getitemid (position ));
}
}
}
}
In the method, when choicemod of listview is set to choice_mode_multiple, mcheckedidstates is used. put (madapter. getitemid (position), Boolean. true); To set the value of the Control. Therefore, we can infer that the items in the listview are selected at a certain time. Therefore, if we set it ourselves, it will be overwritten by listview. We can only use the listview method to modify its status.
Similar to checkbox.
If there is anything wrong, please submit it. Thank you.