AndroidUI system: ListView

Source: Internet
Author: User

AndroidUI system: ListView

1. Create a project: ListViewLearn

2. Modify MainActivity, inherited from ListActivity

3. Create a String array to save the actual content in ListView.

 
 
  1. Package com. learn. listviewlearn. utility;
  2.  
  3. Public class Util {
  4. Public static final String [] COUNTRYS = {"China", "USA", "Russia", "UK", "France "};
  5. }

4. Modify the onCreate method and set an Adapter. The content in the array is displayed in ListView.

 
 
  1. @Override 
  2.   protected void onCreate(Bundle savedInstanceState) { 
  3.       super.onCreate(savedInstanceState); 
  4.       // setContentView(R.layout.activity_main); 
  5.       this.setListAdapter(new ArrayAdapter<String>(this, 
  6.               android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS)); 
  7.   }

Ii. Use the XML layout file to define the ListView Style

1. Modify the activity_main.xml file and add a ListView. The id must be android: id = "@ android: id/list"

 
 
  1. LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical" > 
  5.  
  6.     <ListView 
  7.         android:id="@android:id/list" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="fill_parent" 
  10.         android:background="#FFFFFF00" > 
  11.     </ListView> 
  12.  
  13. </LinearLayout> 

2. Modify the OnCreate Method

 
 
  1. @Override 
  2.   protected void onCreate(Bundle savedInstanceState) { 
  3.       super.onCreate(savedInstanceState); 
  4.       setContentView(R.layout.activity_main); 
  5.       this.setListAdapter(new ArrayAdapter<String>(this, 
  6.               android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS)); 
  7.   }

3. Use an XML layout file to define the ListViewItem Style

1. Create a layout file list_view.xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <ImageView 
  8.         android:id="@+id/imageViewIcon" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.         android:contentDescription="@string/icon" > 
  12.     </ImageView> 
  13.  
  14.     <TextView 
  15.         android:id="@+id/textViewContent" 
  16.         android:layout_width="wrap_content" 
  17.         android:layout_height="wrap_content" > 
  18.     </TextView> 
  19.  
  20. </LinearLayout> 

2. Create an Adapter that inherits from BaseAdapter and mainly modifies the getCount () and getView () methods.

 
 
  1. package com.learn.listviewlearn.adapter; 
  2.  
  3. import com.learn.listviewlearn.R; 
  4. import com.learn.listviewlearn.utility.Util; 
  5.  
  6. import android.content.Context; 
  7. import android.view.LayoutInflater; 
  8. import android.view.View; 
  9. import android.view.ViewGroup; 
  10. import android.widget.BaseAdapter; 
  11. import android.widget.ImageView; 
  12. import android.widget.TextView; 
  13.  
  14. public class ListViewAdapter extends BaseAdapter { 
  15.     private Context context; 
  16.  
  17.     public ListViewAdapter() { 
  18.         // TODO Auto-generated constructor stub 
  19.     } 
  20.  
  21.     @Override 
  22.     public int getCount() { 
  23.         return Util.COUNTRYS.length; 
  24.     } 
  25.  
  26.     @Override 
  27.     public Object getItem(int position) { 
  28.         // TODO Auto-generated method stub 
  29.         return null; 
  30.     } 
  31.  
  32.     @Override 
  33.     public long getItemId(int position) { 
  34.         // TODO Auto-generated method stub 
  35.         return 0; 
  36.     } 
  37.  
  38.     @Override 
  39.     public View getView(int position, View convertView, ViewGroup parent) { 
  40.         if (convertView == null) { 
  41.             convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null); 
  42.             ItemViewCache itemViewCache = new ItemViewCache(); 
  43.             itemViewCache.imageView = (ImageView)convertView.findViewById(R.id.imageViewIcon); 
  44.             itemViewCache.textView = (TextView)convertView.findViewById(R.id.textViewContent); 
  45.             convertView.setTag(itemViewCache); 
  46.         } 
  47.          
  48.         ItemViewCache cache = (ItemViewCache) convertView.getTag(); 
  49.  
  50.         cache.imageView.setImageResource(Util.images[position]); 
  51.         cache.textView.setText(Util.COUNTRYS[position]); 
  52.         return convertView; 
  53.     } 
  54.      
  55.     private static class ItemViewCache{ 
  56.         public TextView textView; 
  57.         public ImageView imageView; 
  58.     } 
  59.  

Util. java

 
 
  1. Package com. learn. listviewlearn. utility;
  2.  
  3. Import com. learn. listviewlearn. R;
  4.  
  5. Public class Util {
  6. Public static final String [] COUNTRYS = {"China", "USA", "Russia", "UK", "France "};
  7. Public static final int [] images = {R. drawable. ic_launcher,
  8. R. drawable. ic_launcher, R. drawable. ic_launcher,
  9. R. drawable. ic_launcher, R. drawable. ic_launcher };
  10. }

4. Add a Click event for the ListView. You only need to implement the onListItemClick () method in MainActivity.

 
 
  1. @ Override
  2. Protected void onListItemClick (ListView l, View v, int position, long id ){
  3. Toast. makeText (this, "you selected" + Util. COUNTRYS [position], Toast. LENGTH_SHORT)
  4. . Show ();
  5. }

Link: http://www.cnblogs.com/zhangtingkuo/archive/2014/08/01/3884689.html

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.