Select multiple items in ListView and select multiple items in listview

Source: Internet
Author: User

Select multiple items in ListView and select multiple items in listview
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 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


How to restrict multiple selection of listview in C #? Only one record can be selected

If you must
Control cannot be selected multiple times
If I have not answered
If you only want to avoid this problem, you can select the first data item by default.
 
In android development, how does one add a single choice and a check box in listview?

You need to redefine a component by yourself. Baidu search custom components should be able to find what you want. If the code is deleted, it will not be rewritten.

Related Article

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.