ApiDemo has an example of multi-choice in listView.
Relatively simple
[Plain]
Package com. example. android. apis. view;
Import android. app. ListActivity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
/**
* This example shows how to use choice mode on a list. This list is
* In CHOICE_MODE_MULTIPLE mode, which means the items behave like
* Checkboxes.
*/
Public class List11 extends ListActivity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetListAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_multiple_choice, GENRES ));
Final ListView listView = getListView ();
ListView. setItemsCanFocus (false );
ListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE );
}
Private static final String [] GENRES = new String [] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama ",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
}
Package com. example. android. apis. view;
Import android. app. ListActivity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
/**
* This example shows how to use choice mode on a list. This list is
* In CHOICE_MODE_MULTIPLE mode, which means the items behave like
* Checkboxes.
*/
Public class List11 extends ListActivity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetListAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_multiple_choice, GENRES ));
Final ListView listView = getListView ();
ListView. setItemsCanFocus (false );
ListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE );
}
Private static final String [] GENRES = new String [] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama ",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
}
The layout simple_list_item_multiple_choice.xml in the framework is a CheckedTextView.
[Html]
<CheckedTextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/text1"
Android: layout_width = "match_parent"
Android: layout_height = "? Android: attr/listPreferredItemHeight"
Android: textAppearance = "? Android: attr/textAppearanceLarge"
Android: gravity = "center_vertical"
Android: checkMark = "? Android: attr/listChoiceIndicatorMultiple"
Android: paddingLeft = "6dip"
Android: paddingRight = "6dip"
/>
<CheckedTextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/text1"
Android: layout_width = "match_parent"
Android: layout_height = "? Android: attr/listPreferredItemHeight"
Android: textAppearance = "? Android: attr/textAppearanceLarge"
Android: gravity = "center_vertical"
Android: checkMark = "? Android: attr/listChoiceIndicatorMultiple"
Android: paddingLeft = "6dip"
Android: paddingRight = "6dip"
/>
In actual application, we will certainly encounter complicated layout. Once complicated layout is used, we find that the native multi-choice mode of listview is invalid! The failure cause can be found by checking the source code of ListView. The problem lies in the setupChild function.
[Java]
Private void setupChild (View child, int position, int y, boolean flowDown, int childrenLeft, boolean selected, boolean recycled ){
..................
If (mChoiceMode! = CHOICE_MODE_NONE & mCheckStates! = Null ){
If (child instanceof Checkable ){
(Checkable) child). setChecked (mCheckStates. get (position ));
}
}
}
Private void setupChild (View child, int position, int y, boolean flowDown, int childrenLeft, boolean selected, boolean recycled ){
..................
If (mChoiceMode! = CHOICE_MODE_NONE & mCheckStates! = Null ){
If (child instanceof Checkable ){
(Checkable) child). setChecked (mCheckStates. get (position ));
}
}
}
The child object is the result returned by the getView () function of the custom adapter. It cannot be operated because the Checkable interface is not implemented. Work und: ListView has a function to check whether the item is selected. In combination with this function, modify the getView function of the custom adapter:
[Java]
Public View getView (int position, View convertView, ViewGroup parent ){
Boolean check = (ListView) parent). isItemChecked (position );
ViewHolder. myCheckedTextView. setChecked (check );
}
Public View getView (int position, View convertView, ViewGroup parent ){
Boolean check = (ListView) parent). isItemChecked (position );
ViewHolder. myCheckedTextView. setChecked (check );
} (O (zookeeper) o alas, the csdn editor is really hard to use. Why can't I delete the messy code at the end)
[Java]
[Plain]
<PRE class = html name = "code" sizcache = "1" sizset = "7"> <PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
</PRE>