1. Define a Checkedtextview in layout
<checkedtextview android:id= "@+id/textview1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:checkmark= "? android:attr/ Listchoiceindicatormultiple " android:gravity=" center_vertical " android:text=" full Name " android:textcolor=" #000 "/>
2, define a adapter, which maintains a sprasebooleanarray, each time GetView (), according to the value stored in the Sprasebooleanarray set check state
class extends baseadapter { // This array records the status of the tick option private Sparsebooleanarray mcheckedtable; Public Myadapter () { new Sparsebooleanarray (); // other code }
3. In the adapter GetView () method, set the checked state according to the value in Sparsebooleanarray
Public View GetView (int position, view Convertview, ViewGroup parent) { = Convertview.findviewbyid ( R.ID.TEXTVIEW1); Checkedview.setchecked (Mcheckedtable.get (position)); // other Code }
4, set a Onitemclicklistener, when the list item is clicked, execute the Checkedtextview.toggle () method, convert the state to come over.
5. Add the new state and position of the Checkedtextview (position key, status value) into the array
Mylistview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {Myadapter adapter=(Myadapter) parent.getadapter (); Checkedtextview name=(Checkedtextview) View.findviewbyid (R.ID.TEXTVIEW1); Name.toggle (); //put the checked item position into a Boolean arrayadapter.getcheckedtable (). Put (position, name.ischecked ()); } });
All selected project information, in Sprasebooleanarray, can be extracted from the data (using the Keyat () and the Valueat () method) through an index and for loop. Value is the true key, which corresponds to the selected item.
An optional listview is defined in Android