In this article, we show that the ListView selects multiple items and implements the filtering function ~
1. Add the following code to the Strings.xml file located under the Res/values folder:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >basicview5</ string> <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello world!</string><string-array name= "Presidents_array" > <item>dwight D. Eisenhower </item> <item>john f. kennedy</item> <item>lyndon B. johnson</item> < Item>richard nixon</item> <item>gerald ford</item> <item>jimmy Carter</item > <item>ronald reagan</item> <item>george h.w. bush</item> <item> Bill clinton</item> <item>george W. bush</item> <item>barack obama</item> </string-array></resources>
2, modify the previous article in the Basicview5.java file code, the revised code is as follows:
Package Com.example.basicview5;import Android.os.bundle;import Android.app.activity;import Android.app.listactivity;import Android.view.menu;import Android.view.view;import Android.widget.ArrayAdapter; Import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Listactivity {String[] presidents;//stores the list information in the Strings.xml file, and then programmatically reads the @overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//---No need to call this---////setcontentview (r.layout.activity_main); ListView ListView = Getlistview ();//Get listactivity List view Listview.setchoicemode (listview.choice_mode_multiple);// You can select multiple items listview.settextfilterenabled (true);//Enable filtering, enter on keyboard, ListView AutoFilter//Getresources () The Presidents method programmatically retrieves the resources that are bundled with the application = Getresources (). Getstringarray (R.array.presidents_array); Setlistadapter (new Arrayadapter<string> (this,android. R.layout.simple_list_item_checked, Presidents));} public void Onlistitemclick (ListView parent, View v, int position, long Id) {Toast.maketext (this, "We have selected" + Presidents[position],toast.length_short). Show (); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
3, the operation of the program, the effect is as follows:
4. Add the following code to the Activity_main.xml file:
< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/ Tools "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "a ndroid:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Tools:context= ". Mainactivity "> <button android:id=" @+id/btn "android:layout_width=" Fill_parent "Android:lay out_height= "Wrap_content" android:onclick= "OnClick" android:text= "Show selected items"/> <listview Android:id= "@+id/android:list" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "/></LINEARLAYOUT>
5. Add the code in the Basicview5.java file as follows:
Package Com.example.basicview5;import Android.os.bundle;import Android.app.activity;import Android.app.listactivity;import Android.view.menu;import Android.view.view;import Android.widget.ArrayAdapter; Import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Listactivity {String[] presidents;//stores the list information in the Strings.xml file, and then programmatically reads the @overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//---need to call this---//setcontentview (r.layout.activity_main);// The active content needs to be loaded because an activity is populated with a ListView part added to the XML file. ListView ListView = Getlistview ();//Get listactivity List view Listview.setchoicemode (listview.choice_mode_multiple);// You can select multiple items listview.settextfilterenabled (true);//Enable filtering, enter on keyboard, ListView AutoFilter//Getresources () The Presidents method programmatically retrieves the resources that are bundled with the application = Getresources (). Getstringarray (R.array.presidents_array); Setlistadapter (new Arrayadapter<string> (this,android. R.layout.simple_list_item_checked, Presidents));} public void Onlistitemclick (ListvieW Parent, View v, int position, long id) {Toast.maketext (this, "We have selected" + Presidents[position],toast.length_sh ORT). Show ();} public void OnClick (view view) {ListView ListView = Getlistview (); String itemsselected = "Selected items: \ n"; for (int i = 0; i < Listview.getcount (); i++) {if (listview.isitemchecked (i)) {//Find the selected itemitemsselected + = Listview.getitematposition (i) + "\ n";//returns the name of the item at the specified location}} Toast.maketext (This, itemsselected, Toast.length_long). Show ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
6, the operation effect is as follows:
Filter and select several items, and then press the button to display the selected items: