The ListView of Android UI

Source: Internet
Author: User

One, created in code (not applicable to XML layout files)

1. Create a project: Listviewlearn

2. Modify Mainactivity, inherit from listactivity

3. Create a string array to hold the actual content in the ListView

 Package com.learn.listviewlearn.utility;  Public class  Util {    publicstaticfinal string[] countrys = {"China", "United States", "Russia", "United Kingdom", "France"  };}

4. Modify the OnCreate method, set a adapter, and the contents of the array are realistic in the ListView

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);         // Setcontentview (r.layout.activity_main);        this. Setlistadapter (thenew arrayadapter<string>(this,                Android. R.layout.simple_dropdown_item_1line, Util.countrys));    }

Ii. using an XML layout file to define the style of the ListView

1. Modify the Activity_main.xml file, add a listview,id must be android:id= "@android: Id/list"

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >    <ListViewAndroid:id= "@android: Id/list"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:background= "#FFFFFF00" >    </ListView></LinearLayout>

2. Modify the OnCreate method

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         this. Setlistadapter (thenew arrayadapter<string>(this,                Android. R.layout.simple_dropdown_item_1line, Util.countrys));    }

Iii. using an XML layout file to define the style of the ListViewItem

1. First create a list_view.xml layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" >    <ImageViewAndroid:id= "@+id/imageviewicon"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:contentdescription= "@string/icon" >    </ImageView>    <TextViewAndroid:id= "@+id/textviewcontent"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" >    </TextView></LinearLayout>

2. Then create a adapter that inherits from Baseadapter, primarily modifying the GetCount () and GetView () methods

 PackageCom.learn.listviewlearn.adapter;ImportCOM.LEARN.LISTVIEWLEARN.R;ImportCom.learn.listviewlearn.utility.Util;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classListviewadapterextendsBaseadapter {Privatecontext Context;  PublicListviewadapter () {//TODO auto-generated Constructor stub} @Override Public intGetCount () {returnUtil.COUNTRYS.length; } @Override PublicObject GetItem (intposition) {        //TODO auto-generated Method Stub        return NULL; } @Override Public LongGetitemid (intposition) {        //TODO auto-generated Method Stub        return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        if(Convertview = =NULL) {Convertview= Layoutinflater.from (context). Inflate (R.layout.list_item,NULL); Itemviewcache Itemviewcache=NewItemviewcache (); Itemviewcache.imageview=(ImageView) Convertview.findviewbyid (R.id.imageviewicon); Itemviewcache.textview=(TextView) Convertview.findviewbyid (r.id.textviewcontent);        Convertview.settag (Itemviewcache); } Itemviewcache Cache=(Itemviewcache) Convertview.gettag ();        Cache.imageView.setImageResource (Util.images[position]);        Cache.textView.setText (Util.countrys[position]); returnConvertview; }        Private Static classitemviewcache{ PublicTextView TextView;  PublicImageView ImageView; }}

Util.java

 Package com.learn.listviewlearn.utility; Import COM.LEARN.LISTVIEWLEARN.R;  Public class Util {    publicstaticfinal string[] countrys = {"China", "United States", "Russia", "United Kingdom", "France" 
   
    };     
     Public 
    Static 
    Final 
    int [] images =
     {r.drawable.ic_launcher,            r.drawable.ic_launcher, R.drawable.ic_launcher,            r.drawable.ic_ Launcher, R.drawable.ic_launcher};}
   

Iv. Add the Click event for the ListView. You only need to implement the Onlistitemclick () method in Mainactivity

    @Override    protectedvoidintlong  ID) {        Toast.maketext (This, "You have selected" + util.countrys[position], Toast.length_short)                . Show ();    

Source: Hellolistview.zip

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.