ListView of common Android UI components (2) -- custom ListView
This article introduces how to select multiple items and filter items in ListView ~
1. Add the following code to the strings. xml file in the res/values Folder:
BasicView5
Settings
Hello world!
Dwight D. Eisenhower
John F. Kennedy
Lyndon B. Johnson
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George H.W. Bush
Bill Clinton
George W. Bush
Barack Obama
2. modify the code of the BasicView5.java file in the previous article. The modified 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; // store the list information in strings. in the xml file, read @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // --- no need to call this --- // setContentView (R. layout. activity_main); ListView listView = getListView (); // gets the list view of ListActivity listView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE); // You can select multiple items for listView. setTextFilterEnabled (true); // enable the filtering function. Enter it on the keyboard and the ListView will automatically filter. // getResources () the method is to programmatically retrieve the resource presidents = getResources () bound to the application (). getStringArray (R. array. presidents_array); setListAdapter (new ArrayAdapter
(This, android. r. layout. simple_list_item_checked, presidents);} public void onListItemClick (ListView parent, View v, int position, long id) {Toast. makeText (this, "You 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 present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
3. Run the program with the following effect:
4. Add the following code to the activity_main.xml file: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;">
5. Add the code to 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; // store the list information in strings. in the xml file, read @ Overrideprotected void onCreate (Bundle SavedInstanceState) {super. onCreate (savedInstanceState); // --- need to call this --- // setContentView (R. layout. activity_main); // because a ListView part is added to the xml file to fill in an activity, the activity content needs to be loaded. ListView listView = getListView (); // obtain the List View of ListActivity listView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE); // You can select multiple items for listView. setTextFilterEnabled (true); // enable the filtering function. Enter it on the keyboard and the ListView will automatically filter. // getResources () the method is to programmatically retrieve the resource presidents = getResources () bound to the application (). getStringArray (R. array. presidents_array); setListAdapter (new ArrayAdapter
(This, android. r. layout. simple_list_item_checked, presidents);} public void onListItemClick (ListView parent, View v, int position, long id) {Toast. makeText (this, "You have selected" + presidents [position], Toast. LENGTH_SHORT ). 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"; // return the region name of the specified position} 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 present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
6. The running effect is as follows:
Filter and select several items, and then press the button to display the items selected: