Android--ListView and adapter

Source: Internet
Author: User

The ListView has a very important role in Android. The ListView in Android Development is a more commonly used component that displays specific content as a list, and can be displayed adaptively based on the length of the data.

Background

Built a person class with name,number,id, three attributes.

Private String name;     Private String number;     Private int ID;

Used primarily to add information to the ListView.

Layout

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity "android:orientation= "vertical">    <ListViewAndroid:id= "@+id/lv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"         /></LinearLayout>

Just put the ListView up and it's OK.

Program

Private ListView LV;
Private list<person> List;

@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); List=NewArraylist<person>(); LV=(ListView) Findviewbyid (r.id.lv);        Addperson (); Lv.setadapter (NewMyadapter ()); }    Private classMyadapterextendsBaseadapter {@Override Public intGetCount () {//return size            returnlist.size (); } @Override PublicObject GetItem (intposition) {            //TODO Auto-generated method stubs            return NULL; } @Override Public LongGetitemid (intposition) {            //TODO Auto-generated method stubs            return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {TextView TV=NewTextView (Getapplicationcontext ()); Tv.settextsize (50);            Tv.settextcolor (Color.Blue); Person Person=List.get (position);            Tv.settext (Person.tostring ()); System.out.println ("Return position" +position); returnTV}} //Adding data functions    Private voidAddperson () { for(inti = 0; I < 20; i++) {person Person1=NewPerson ("Zhang San" + I, "12345678912", i);        List.add (Person1); }    }

To declare a adapter,adapter inside the data, then the ListView configures the adapter through Setadapter.

----------------------------Simple Split-line------------------------------------Simple---------------------------

If you need to customize the style in the Lixtview, you can have the layout of the item layout.

Item layout

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "60dip"android:orientation= "Horizontal" >    <TextViewAndroid:id= "@+id/tv_id"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "5dip"Android:text= "id"Android:textcolor= "#ff0000"android:textsize= "18SP" />    <LinearLayoutAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"android:orientation= "vertical" >                <TextViewAndroid:id= "@+id/tv_name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "5dip"Android:text= "First Name"Android:textcolor= "#000000"android:textsize= "18SP"/>        <TextViewAndroid:id= "@+id/tv_phone"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "5dip"Android:text= "Phone"Android:textcolor= "#88000000"android:textsize= "16SP"/>    </LinearLayout></LinearLayout>

Re-write the GetView method inside the adapter:

GetView

@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { person person=List.get (position); View View= View.inflate (mainactivity. This, R.layout.listview_item,NULL); //Find IDTextView tv_id =(TextView) View.findviewbyid (r.id.tv_id); Tv_id.settext ("ID:" +Person.getid ()); TextView Tv_name=(TextView) View.findviewbyid (r.id.tv_name); Tv_name.settext ("Tv_name:" +person.getname ()); TextView Tv_phone=(TextView) View.findviewbyid (R.id.tv_phone); Tv_phone.settext ("Tv_phone:" +Person.getnumber ()); returnview; }

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

ListView Learning. zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.