Basic android beginners ListView view (8)

Source: Internet
Author: User

I. ListView View:

ListView is used to display a list control.

Public class MainActivity extends ListActivity {String [] str = {"China", "France", "UK", "Switzerland", "Austria", "USA", "India ", "Vatican"}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setListAdapter (new ArrayAdapter
 
  
(This, android. r. layout. simple_list_item_1, str);} @ Overrideprotected void onListItemClick (ListView l, View v, int position, long id) {// TODO Auto-generated method stubToast. makeText (this, "you clicked" + str [position], Toast. LENGTH_SHORT ). show ();}}
 

This is the most basic list of ListView implementations:

MainActivity extends the ListView class, while the ListView class extends the Activity class and displays a list of items by binding to a data source. Note that you do not need to add the ListView control to the layout file, because the ListView class already contains a ListView. In the onCreate () method, use setListAdapter () method to use a ListView to program the entire screen of the activity. ArrayAdapter object management is a string array displayed by ListView. When you click a list option in ListView, The onListItemClick () method is triggered to display a piece of toast information.


Running diagram:


We can store the strings to be displayed in strings. xml:

 
     
  
   
ListViewTest
      
  
   
Settings
      
  
   
Hello world!
      
          
   
    
China
           
   
    
France
           
   
    
UK
           
   
    
Switzerland
           
   
    
Austria
           
   
    
USA
           
   
    
India
           
   
    
Vatican
           
   
    
Maldives
       
  
 


MainActivity. java:

Public class MainActivity extends ListActivity {String [] str; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); str = getResources (). getStringArray (R. array. str); setListAdapter (new ArrayAdapter
 
  
(This, android. r. layout. simple_list_item_1, str);} @ Overrideprotected void onListItemClick (ListView l, View v, int position, long id) {// TODO Auto-generated method stubToast. makeText (this, "you clicked" + str [position], Toast. LENGTH_SHORT ). show ();}}
 


You can use the getResources () method to programmatically retrieve resources bound to an application.


Custom ListView + custom adapter (BaseAdapter ):

ListView layout:

     
      
    
   

MainActivity. java:

Public class MainActivity extends ListActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // set an Adapter and use the custom Adapter setListAdapter (new TextImageAdapter (this);} private class TextImageAdapter extends BaseAdapter {private Context mContext; public TextImageAdapter (Context context) {this. mContext = context;}/*** number of elements */public int getCount () {return left. length;} public Object getItem (int position) {return null;} public long getItemId (int position) {return 0 ;} // generate View public View getView (int position, View convertView, ViewGroup parent) elements displayed in ListView {// optimize ListView if (convertView = null) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. activity_main, null); ItemViewCache viewCache = new ItemViewCache (); viewCache. mTextLeft = (TextView) convertView. findViewById (R. id. textLeft); viewCache. mTextRight = (TextView) convertView. findViewById (R. id. textRight); convertView. setTag (viewCache);} ItemViewCache cache = (ItemViewCache) convertView. getTag (); // set the text and image, and then return this View for the ListView Item display cache. mTextLeft. setText (left [position]); cache. mTextRight. setText (Right [position]); return convertView;} private static class ItemViewCache {public TextView mTextLeft; public TextView mTextRight ;} // TextView private String [] left = new String [] {"country", "Province", "region"} on the left of the screen "}; // TextView private String [] Right = new String [] {"China", "Shandong", "Binzhou "};}


We have defined the adapter here, which will be explained in detail later. Click events to add a list:

@ Overrideprotected void onListItemClick (ListView l, View v, int position, long id) {// TODO Auto-generated method stubToast. makeText (this, "you clicked" + left [position] + Right [position], Toast. LENGTH_SHORT ). show ();}

Running effect:




Zookeeper

Related Article

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.