ListView of common Android UI components (2) -- custom ListView

Source: Internet
Author: User

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:



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.