Multiple choice/single choice for listview

Source: Internet
Author: User
The listview itself has a single-choice and multi-choice mode. You can set it through listview. setchoicemode:
Listview. setchoicemode (listview. choice_mode_multiple); // enable multi-choice listview. setchoicemode (listview. choice_mode_single); // enables single-choice listview. setchoicemode (listview. choice_mode_none); // default mode listview. setchoicemode (listview. choice_mode_multiple_modal); // I have never used it. I don't know what it is.
Single choiceRequired: listview. setchoicemode (listview. choice_mode_single );
The listview control also needs to specify a selector: Android: listselector = "@ drawable/checkable_item_selector"
<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@color/c1" android:state_pressed="true"/>    <item android:drawable="@color/c1" android:state_checked="true"/>    <item android:drawable="@color/c2"/></selector>
However, the selected color is the system-selected color instead of the specified C1. Why?
Implement multiple choiceSettings: listview. setchoicemode (listview. choice_mode_single );
You need to implement the checkable interface for the view of each item. The following is the checkable interface implemented by linearlayout:
Public class extends linearlayout implements checkable {private Boolean mchecked; Public checkablelinearlayout (context, attributeset attrs) {super (context, attrs) ;}@ override public void setchecked (Boolean checked) {mchecked = checked; setbackgrounddrawable (checked? New colordrawable (0xff1_a0): NULL); // blue when elected} @ override public Boolean ischecked () {return mchecked;} @ override public void toggle () {setchecked (! Mchecked );}}
Use:
<com.ljfbest.temp.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:minHeight="?android:attr/listPreferredItemHeightSmall"        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"        android:paddingStart="?android:attr/listPreferredItemPaddingStart"        android:textAppearance="?android:attr/textAppearanceListItemSmall" /></com.ljfbest.temp.CheckableLinearLayout> 
The demo diagram is attached as follows:

The following are several APIs for obtaining/setting information about the selected entries: listview. getcheckeditemcount (); // get the selected number of rows: the value of choice_mode_none is invalid. listview. getcheckeditemposition (); // gets the selected row. The value is valid only in single-choice mode and intlistview is returned. getcheckeditemids (); // obtain the IDs of the selected items, which is of the long [] type. Note that the hasstableids () of the adapter returns true, and these IDs are the getitemid (INT position) of the adapter) returned. the demo shows the listview. setitemchecked (Position, value); // you can specify the status of a row.


In addition, the following error may be reported when listview is used:Java. Lang. classcastexception: Android. widget. headerviewlistadapter cannot be cast to Android. widget. simpleadapterlistview has headerview/footerview, and its original adapter will be encapsulated into headerviewlistadapter:

Listadapter used when a listview has header views. this listadapter wraps another one and also keeps track of the header views and their associated data objects. this is intended as a base class; you will probably not need to use this class directly in your own code.
You can obtain the original adapter in the following ways: headerviewlistadapter hadapter = (headerviewlistadapter) listview. getadapter (); myadapter my = (myadapter) hadapter. getwrappedadapter ();
Therefore, when a header/footer is added, lv_data.getadapter ()). getwrappedadapter (). getcount () and (headerviewlistadapter) listview. getadapter ()). getwrappedadapter (). getcount () is 1 different
Demo address:Http://download.csdn.net/detail/ljfbest/8109731

Multiple choice/single choice for listview

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.