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.