Android learning-View list (ListView and ListActivity)

Source: Internet
Author: User

Android learning-View list (ListView and ListActivity)

Note:

View lists (ListView and ListActivity) are similar to AutoComplete and Spinner. They all need a list item for display. You can use the content Adapter to provide a list item for display.

You can create a ListView using either of the following methods:

(1) directly use ListView to create

(2) The Activity inherits the ListActivity


Common XML attributes of ListView


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + CjwvcD4KPHA + z8LD5rK8vtbBvbj2bGlzdFZpZXejrNK7uPa7 + signature + CjxwcmUgY2xhc3M9 "brush: java;">

(2) strings. xml stores the array of listView1 content

 
     
  
   
ListView
      
  
   
Settings
      
  
   
Hello world!
      
          
   
    
Chinese
           
   
    
Korean
           
   
    
English
           
   
    
Japanese
           
   
    
Portuguese
           
   
    
Russian
       
  
 



(3) MainActivity. java

Steps:

1. Retrieve layout ListView

2. encapsulate the display content list or Array

3. Build an Adapter

4. Add an adapter to the ListView

Package com. example. listview; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity {private ListView listView2 = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain ListViewlistView2 = (ListView) findViewById (R. id. listView2); // defines the array String arr [] = new String [] {"China", "Korea", "Japan", "USA", "Portugal ", "Russia"}; // declare the adapter // this context // android. r. layout. simple_list_item_checked list style // arr display content (array or list set) ArrayAdapter
 
  
ArrayAdapter = new ArrayAdapter
  
   
(This, android. R. layout. simple_list_item_checked, arr); // listView Add the adapter listView2.setAdapter (arrayAdapter); Combine (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView)
   Parent, View view, int position, long id) {Log. I ("listView", parent. getItemAtPosition (position). toString ());}});}}
  
 

The running effect is as follows:




If you want to customize the list, the list items display multiple components. We can use SimpleAdapter to customize our list.

(1) main_activity.xml

The layout of the ImageView and TextView components is used to display the list content.

      
         
          
       
        
        
  
 

(2) The MainActivity. java steps are the same as those above.

Package com. example. listviews; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. widget. listView; import android. widget. simpleAdapter; public class MainActivity extends Activity {private ListView listView = null; // defines the ListView component @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the listView component listView = (ListView) findViewById (R. id. listView); // initialize the basic data String name [] = new String [] {"Zhang San", "Li Si", "Wang Wu", "Zhang Fei "}; string phone [] = new String [] {"14313426573", "15908263541", "18012345678", "13423456789"}; int img [] = new int [] {R. drawable. name01, R. drawable. name02, R. drawable. name03, R. drawable. name04}; // encapsulate the list set data List
 
  
> List = new ArrayList
  
   
> (); For (int I = 0; I
   
    
ListItem = new HashMap
    
     
(); ListItem. put ("img", img [I]); listItem. put ("name", name [I]); listItem. put ("phone", phone [I]); list. add (listItem);} // configure SimpleAdapter // this context // R. layout. activity_main layout file // new String [] {"img", "name", "phone"} List content key // new int [] {R. id. img, R. id. name, R. id. phone} The layout component SimpleAdapter simpleAdapter = new SimpleAdapter (this, list, R. layout. activity_main, new String [] {"img", "name", "phone"}, new int [] {R. id. img, R. id. name, R. id. phone}); // Add the adapter listView. setAdapter (simpleAdapter );}}
    
   
  
 

The running effect is as follows:



In addition to SimpleAdapter, we can also override the getView method of BaseAdapter as the list item. If you are interested, you can write a BaseAdapter.




Method 2: The Activity inherits the ListActivity


Note:

ListActivityThe default layout consists of a full screen list in the center of the screen. If you do not want to use the default layout, you canOnCreate ()TheSetContentView ()Method to set your own layout. If you specify a custom layout, your layout must containIdIs"@ Id/android: list" ListView. If you specifyIdIsView of "@ id/android: empty", WhenListViewWhen no data is displayedViewIt will be displayed.ListViewWill be hidden


(1) main_activity.xml

A ListView and TextView are laid out. The id of TextView is@ Id/android: empty, WhenListViewWhen no data is displayed, the TextView is displayed.

     
      
      
 

(2) MainActivity. java

Package com. example. listactivity; import android. app. listActivity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends ListActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String arr [] = new String [] {"China", "Korea", "Japan", "USA", "Portugal", "Russia "}; // set the adapter ArrayAdapter
 
  
ArrayAdapter = new ArrayAdapter
  
   
(This, android. R. layout. simple_list_item_checked, arr); // Add the adapter setListAdapter (arrayAdapter) to ListActivity );}}
  
 



If the ListView has no content, TextView is displayed. As follows:





Method 1 and method 2 have the same effect. The difference is that one adds an adapter to the ListView component and the other adds an adapter to the ListActivity class.

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.