Android Program Development: (12) List Control-12.2 extended ListView Function

Source: Internet
Author: User

ListView is a view that can be expanded in depth. During project creation, it is essential to extend the ListView to display data. Next, we will show you how to select multiple objects in ListView and how to use the "filter" function of ListView.

1. Use the project BasicViews5 in the previous section.

2. Add some code in BasicViews5Activity. java.

[Java]
String [] presidents;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// --- No need to call this ---
// SetContentView (R. layout. main );
 
ListView lstView = getListView ();

// LstView. setChoiceMode (ListView. CHOICE_MODE_NONE );
// LstView. setChoiceMode (ListView. CHOICE_MODE_SINGLE );
LstView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE );
LstView. setTextFilterEnabled (true );
 
Presidents =
GetResources (). getStringArray (R. array. presidents_array );
 
SetListAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_checked, presidents ));
}
3. Press F11 to debug the simulator. Now, you can click every object in the ListView and it will be displayed on the right side of it. It has been selected.

 

The reference of ListView is obtained through the getListView () method. This is because we want to modify the behavior characteristics of ListView by encoding.

In this case, use the setChoiceMode () method to set the response method for the ListView to respond to the user click event. For example, when ListView. CHOICE_MODE_MULTIPLE is set, you can select multiple options: www.2cto.com.

[Java]
ListView lstView = getListView ();

// LstView. setChoiceMode (ListView. CHOICE_MODE_NONE );
// LstView. setChoiceMode (ListView. CHOICE_MODE_SINGLE );
LstView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE );

Another amazing feature of ListView is "filter ". After activating the "filter" function using the setTextFilterEnable () method, you can enter text on the keyboard. Meanwhile, the ListView will automatically match the information entered by the user:
[Java]
LstView. setTextFilterEnabled (true );

All information with "jhon" is filtered out.


 

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.