An optional listview is defined in Android

Source: Internet
Author: User

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

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.