Customizing the ListView Adapter

Source: Internet
Author: User

Inheriting the Baseadapter class

Cover the following 4 methods:

@Override Public intGetCount () {returnusers.size (); } @Override PublicObject GetItem (intposition) {        returnUsers.get (position); } @Override Public LongGetitemid (intposition) {        return(User) GetItem (position)). GetId (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        if(convertview==NULL) {Convertview=inflater.inflate (Resouce,NULL); }        //get the appropriate controlTextView id=(TextView) Convertview.findviewbyid (r.id.user_id); TextView name=(TextView) Convertview.findviewbyid (r.id.user_name); TextView Phone=(TextView) Convertview.findviewbyid (R.id.user_phone); User User=(User) getItem (position); //binding DataId.settext (string.valueof (User.getid ()));        Name.settext (User.getname ());        Phone.settext (User.getphone ()); returnConvertview; }

The above code implements a simple ListView adapter,

GetCount () Gets the total number of bars for the current data

GetItem () Gets the specific object that can get the data according to position

Getitemid () Gets the ID of the data that is specific to the position

GetView () View object for item after binding data is obtained based on position and Convertview

The GetView () method can be slightly optimized in the following ways:

 PublicView GetView (intposition, View Convertview, ViewGroup parent) {        if(convertview==NULL) {Convertview=inflater.inflate (Resouce,NULL); //Cache Control ObjectViewcontrol views=NewViewcontrol (); Views.id=(TextView) Convertview.findviewbyid (r.id.user_id); Views.name=(TextView) Convertview.findviewbyid (r.id.user_name); Views.phone=(TextView) Convertview.findviewbyid (R.id.user_phone);        Convertview.settag (views); } Viewcontrol views=(Viewcontrol) Convertview.gettag (); User User=(User) getItem (position);        Views.id.setText (String.valueof (User.getid ()));        Views.name.setText (User.getname ());        Views.phone.setText (User.getphone ()); returnConvertview; }        Private Final classviewcontrol{ PublicTextView ID;  PublicTextView name;  PublicTextView Phone; }

Personal Understanding:

Adapter in fact, and the computer network adapter, a kind of can be rubbed and two have a certain connection but not directly simple to join together. This is where you assume the ability to bind data to controls.

Customizing the ListView Adapter

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.