Android Notes (10) ListView

Source: Internet
Author: User

The ListView is one of the most commonly used controls in Android and the most difficult to use.
The role of the ListView is to show a large amount of data, but the ListView does not work directly with the data, but through the adapter as an intermediate bridge.
1. First create a ListView layout file in Layout

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent" >             <ListViewandroid:id="@+id/list_view"android:layout_width="Match _parent "android:layout_height=" Match_parent " >                            </ListView></linearlayout>

2. Then, create a child layout for the ListView

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent" >             <ImageViewandroid:id="@+id/num_image"android:layout_width= "Wrap_content" android:layout_height="wrap_content" />                            <TextViewandroid:id= "@+id/num_name"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_gravity=" Center "android:layout_marginleft=" 10dip " />                                        </linearlayout>

3. Then define an entity class as the adapter type for the ListView adaptor

publicclass Number {    private String name;    privateint imageId;    publicNumberint imageId) {        this.name = name;        this.imageId = imageId;    }    publicgetName() {        return name;    }    publicintgetImageId() {        return imageId;    }}

4. Build the adapter

 Public  class numberadapter extends arrayadapter<number > {    Private intRsId; PublicNumberadapter (Context context,intItemid, list<number> numlist) {Super(context, Itemid, numlist);    rsId = itemid; } @Override PublicView GetView (intPosition, View Convertview, ViewGroup parent) {//TODO auto-generated method stubNumber num = GetItem (position);        View view; Viewsave Viewsave;//Create a class to save instances of ImageView and textview without having to regenerate the instance every time        if(Convertview = =NULL{view = Layoutinflater.from (GetContext ()). Inflate (RsId,NULL); Viewsave =NewViewsave ();            Viewsave.image = (ImageView) View.findviewbyid (r.id.num_image);            Viewsave.name = (TextView) View.findviewbyid (r.id.num_name);        View.settag (Viewsave); }Else{view = Convertview;        Viewsave = (viewsave) view.gettag ();        } viewsave.image.setImageResource (Num.getimageid ()); Viewsave.name.setText (Num.getname ());returnView } class viewsave {ImageView image;    TextView name; }}

5. Code for the activity

 Public  class mainactivity extends actionbaractivity {    PrivateList<number> numlist =NewArraylist<number> ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.list_layout);        Initnumber (); Numberadapter adapter =NewNumberadapter (mainactivity. This, R.layout.item, numlist);        ListView list = (ListView) Findviewbyid (R.id.list_view);        List.setadapter (adapter); List.setonitemclicklistener (NewOnitemclicklistener () {@Override             Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Number num = Numlist.get (position); Toast.maketext (mainactivity. This, Num.getname (), Toast.length_short). Show ();    }            }); }Private void Initnumber() {Number N1 =NewNumber ("N1", R.DRAWABLE.N1);        Numlist.add (N1); Number N2 =NewNumber ("N2", R.DRAWABLE.N2);        Numlist.add (N2); Number N3 =NewNumber ("N3", R.DRAWABLE.N3);        Numlist.add (n3); Number N4 =NewNumber ("N4", R.DRAWABLE.N4);        Numlist.add (N4); Number N5 =NewNumber ("N5", R.DRAWABLE.N5);        Numlist.add (N5); Number N6 =NewNumber ("N6", R.DRAWABLE.N6);        Numlist.add (N6); Number N7 =NewNumber ("N7", r.drawable.n7);        Numlist.add (N7); Number N8 =NewNumber ("N8", R.DRAWABLE.N8);        Numlist.add (N8); Number N9 =NewNumber ("N9", R.DRAWABLE.N9);        Numlist.add (N9); Number N10 =NewNumber ("N10", R.DRAWABLE.N10);             Numlist.add (N10); }    }

6. The final realization of the

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Note (10) ListView

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.